UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » Socket accessed by Multiple Threasds

#1 2007-01-29 09:43 AM

kumar.shanmugam
Guest

Re: Socket accessed by Multiple Threasds

Hi,

We have a requirement that the same socket created in client should be accessed by two task running simultanesously.

Task One will do Select and then Receive only

Task Two will do only Send Operation.

They are totally independent task.

What kind of protection we need to have have for these kind of Socket Operations.

Thanks in advance,

#2 2007-01-29 07:12 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Socket accessed by Multiple Threasds

You shouldn't really need any mutex or anything, if that's what you're asking...  It
should be safe enough to have one thread reading and another writing to the same
socket...  If you were going to have multiple readers or writers, then you might have
issues to worry about, but this scheme should be fine without anything special...

Offline

#3 2007-04-29 06:21 AM

dhruvm
Member
Registered: 2007-04-29
Posts: 3

Re: Socket accessed by Multiple Threasds

However, you _may_ want to look into how signals behave with threads around.

Offline

#4 2007-04-29 05:48 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Socket accessed by Multiple Threasds

Who said anything about signals??  In general, if you're writing multi-threaded
code, I'd recommend avoiding ALL signals, if possible...  (In fact, even for regular
old single-threaded code, I try to avoid signals whenever possible...  They're just
plain ugly, and there are usually superior methods available for accomplishing the
same task...  The only really unavoidable signal-handler I often need is a SIGCHLD
handler, if creating any child processes...  Not counting debugging stuff like SIGSEGV
handlers, which cause total program death when triggered anyway, so aren't really
part of the normal, expected run-time behavior...)

Offline

#5 2007-06-11 08:44 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Socket accessed by Multiple Threasds

This is an old thread but gonna add a reply...

Cause well - I haven't been posting recently... and my post count is way down ...  and it is in the C++ section so if you are writing code in C++ there may an issue...

The issue is NOT sychronization in general at the kernel socket / buffer level - as long as you are using a OS circa say 1998 and later...

But...

If you are using any C++ streams / buffers... you SHOULD be ordering access to them... or at least making certain your compiler isn't doing anything strange... but pretty much this just means making certain you set your buffers to a size larger than your largest packet / response size and fully flush after each packet...

For signals - going Rob's route - if you are doing multi-threaded then avoid them... personally I don't think signals are bad ( e.g. I have been using aio / edge trigger events ) ...

But...

Some older systems ( e.g. Linux without the new thread lib ) do NOT handle signals correctly... and if you want to handle even simple situations such as EPIPE properly e.g. to perform a clean shutdown, you have to have handlers in EVERY thread... and leave it unblocked EVERYWHERE...  or wrap every read /write ( again - in every thread - to recognize the issue ) and handle it appropriately... :-/


Michael

P.S. What is my thread count at now - darn - still too low...


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#6 2007-06-11 12:19 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Socket accessed by Multiple Threasds

Offline

#7 2008-05-09 03:51 AM

clint999
Guest

Re: Socket accessed by Multiple Threasds

Removed

Signature spammers == bad.

Please advertise on your own site.

#8 2008-05-29 11:38 PM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: Socket accessed by Multiple Threasds

Offline

#9 2008-05-30 02:30 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Socket accessed by Multiple Threasds

Yes you can. Multiple threads (and processes) can read and write to the same socket
at the same time, but it's only safe without locking if there's only one thread reading
and only one thread writing if it's a streaming socket like TCP (UDP should be
safe). Multiple threads both reading and writing in general won't work, because one
thread can receive half a message while the other gets the rest, and except when
you're interested in individual bytes that isn't something your application can
handle. Same for writing, it can get mixed up, and you don't know where or how.
Packet based sockets like UDP should work because there one write or read
corresponds with one UDP socket, so as long as the order of the packets doesn't
matter it should work. If it's also a good idea is another matter.

Offline

#10 2008-05-30 07:38 AM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: Socket accessed by Multiple Threasds

is this a common behavior for Linux, win32 and BSD* systems?

Offline

#11 2008-05-30 10:29 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Socket accessed by Multiple Threasds

Don't know about win32, but for the others, yes. It's true for all systemcalls,
you can call them safely at the same time, though your application won't be
always able to handle the resulting behaviour.

Offline

  • Index
  • » C++
  • » Socket accessed by Multiple Threasds

Board footer

Powered by FluxBB