LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2011, 09:53 AM   #1
darshan82
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Rep: Reputation: 0
Question Multiple threads reading from same socket.


Hi,

Is it fine if i have a single server fd which is read by multiple threads ?
Like i will have a one-to-many server and the recv is called in multiple threads (since there is no connect/accept prob with one to many server). Also will a blocking and non-blocking fd make a difference in this case?

Actually I have randomly tried this which works ok, but I am extremely skeptical. If there is some mutex lock inside recv, then i might not achieve any gain at all.

Darshan.
 
Old 03-06-2011, 11:25 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. Yes, any arbitrary thread may read any open socket at any time.

2. Yes, any thread that reads a blocking socket will block until there is data to be read.

3. No, one thread blocking on a read will not affect any other thread; nor affect the socket.

4. If there are *multiple* threads blocking, waiting to read from the *same* socket ... that's bad

5. In TCP/IP sockets, you cannot absolutely guarantee that *any* single read will return all the data you expect in a single call. It might take multiple reads to get all the data. This might wreak havoc with your "multiple threads reading/writing to the same socket" scenario if you're not careful.
 
Old 03-07-2011, 11:04 PM   #3
darshan82
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Hi,

Thanks a lot for the answer.

Further,

4. If there are *multiple* threads blocking, waiting to read from the *same* socket ... that's bad
Yes, you are right. But I have other threads to do the normal chores of the application. And so it should be fine.

5. In TCP/IP sockets, you cannot absolutely guarantee that *any* single read will return all the data you expect in a single call. It might take multiple reads to get all the data. This might wreak havoc with your "multiple threads reading/writing to the same socket" scenario if you're not careful.
Ok. I am currently using SCTP. So every read fetches me a full message.

Regards,
Darshan.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Multiple threads in socket problem c++ vbx_wx Programming 5 11-26-2010 08:08 PM
OpenGL:Multiple windows in multiple threads Hamed Programming 0 09-16-2010 08:36 AM
Problems with using same socket in different threads montylee Programming 3 08-18-2008 04:45 AM
reading pages of threads after a search rblampain LQ Suggestions & Feedback 4 11-16-2005 09:49 PM
after reading most of the threads boxerboy *BSD 10 08-23-2005 09:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 07:41 PM.

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