LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-05-2008, 04:26 AM   #1
fzangheri
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Rep: Reputation: 0
How thread-safe are AF_INET sockets?


Hi all,

This is my first post to this forum.

I'm writing an application in which I need to have "thread A" to receive UDP packets and "thread B" to send UDP packets, using the same socket.

Kernel: 2.6.20.x .

The question is, is it safe to have B calling sendto() while A is blocked in recvfrom() on the same socket?

Thank you in advance,
best regards.

--
Filippo
 
Old 08-05-2008, 05:56 AM   #2
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
yes it should be possible.

create the socket in thread A.

while creating the thread B, pass this as parameter or save it in a global variable so that both the threads will be able to access it.

care should be take for proper synchronization.
 
Old 08-05-2008, 11:26 AM   #3
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
You might actually have to make the socket non-blocking. This would require a slight change to thread A to check for data on the socket since it would no longer just block on the recvfrom.
 
Old 08-05-2008, 01:48 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
I don't see why non-blocking sockets are needed here...
 
Old 08-05-2008, 03:56 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I do what OP is trying to do all the time. I almost always use non-blocking sockets with select for reading, though it does complicate writing if the other end is too slow to clear the buffer. I generally prefer non-blocking because it forces you to consider timing, which is important to threaded socket programs. Also, blocking isn't always reliable by itself since signals in one thread can cause system calls in another thread to interrupt, causing an EINTR, especially when using sockets.
ta0kira
 
Old 08-06-2008, 05:04 AM   #6
fzangheri
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Thank you all for your replies.

I've tested a lot this application using blocking sockets and didn't get any single error. So, I guess it's safe to use blocking sockets.

Btw, can you give me an example usage of non-blocking sockets and select() for major robustness?
Thanks.

Best regards.

--
Filippo
 
Old 08-06-2008, 05:24 AM   #7
swift2008
Member
 
Registered: Jul 2008
Posts: 78

Rep: Reputation: 15
Quote:
Originally Posted by fzangheri View Post
Thank you all for your replies.

I've tested a lot this application using blocking sockets and didn't get any single error. So, I guess it's safe to use blocking sockets.

Btw, can you give me an example usage of non-blocking sockets and select() for major robustness?
Thanks.

Best regards.

--
Filippo
hi

if you don't mine.
can i have you have compiled socket programming code..
that you are using threads..

may be it will helpful for me.
 
Old 08-06-2008, 05:29 AM   #8
fzangheri
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by swift2008 View Post
hi

if you don't mine.
can i have you have compiled socket programming code..
that you are using threads..

may be it will helpful for me.

Hi swift2008,

May I ask you to reformulate your question, please? I'm not an English native speaker and I'm not sure I understand correctly your post.

Thanks.

--
Filippo
 
Old 08-06-2008, 05:46 AM   #9
swift2008
Member
 
Registered: Jul 2008
Posts: 78

Rep: Reputation: 15
Quote:
Originally Posted by fzangheri View Post
Hi swift2008,

May I ask you to reformulate your question, please? I'm not an English native speaker and I'm not sure I understand correctly your post.

Thanks.

--
Filippo
can i see your socket program code.
 
Old 08-06-2008, 06:30 AM   #10
fzangheri
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Post

Quote:
Originally Posted by swift2008 View Post
can i see your socket program code.
Yes.

After removing all synchronization and communication mechanisms, the code looks kind of this:

Code:
/*
 * Thread A
 */
void *A()
{
	// Initialize UDP socket:
	sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
		
	// Bind socket
	
	while (1)
	{
		// Wait for UDP packet:
		recvfrom(rdfsock, rdfbuf, MAX_PACKET_SIZE, 0, (struct sockaddr *)&peerAddr, &peerAddr_len);
		// Send message to thread B
	}
	return NULL;
}

/*
 * Thread B
 */
void *B()
{
	while (1)
	{
		// Receive request from "A" via internal mechanism
		// Elaborate request and create response
		// Send response:
		sendto(sock, response, strlen(response), 0, (struct sockaddr *)&peerAddr, peerAddr_len);
	}
	return NULL;
}

int main()
{	
	pthread_t thread_A, thread_B;
	
	pthread_create(&thread_A, NULL, A, NULL);	
	pthread_create(&thread_B, NULL, B, NULL);
	pthread_join(thread_A, NULL);
	pthread_join(thread_B, NULL);
	
	pthread_exit(NULL);
	exit(0);
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python thread safety: printing from thread to redirected stdout - safe? BrianK Programming 2 10-11-2010 11:28 AM
Is glibc mtrace thread-safe? wielemaker Programming 0 12-19-2007 03:09 PM
making fuction thread safe knobby67 Programming 3 12-08-2007 12:02 AM
is Pam Thread Safe??? ptobra Programming 4 07-24-2007 09:19 PM
What is thread safe....??? rajsun Programming 5 04-26-2005 11:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration