Thread: Multiple Threads, One listener.

  1. #1
    Registered User
    Join Date
    Nov 2004

    Multiple Threads, One listener.

    I have a situation where I have multiple threads sending data over a socket. The intended recipients should accept the data, process it, and send it back to the original sender. I have thought of two configs. One where there is just one listening port on the sender side and this port forwards the received data to the thread which shot off the request in the first place. This requires an additional thread_id field to be sent over the network. The second would be that each thread gets its own port for sending and receiving data. This limits the number of simultaneous requests over the net. Is there some other config that I can look into? I am using unix sockets and posix threads using C / C++.

    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    Not that I have experience doing exactly this but I would say go with the former idea (one listener and an ID for recipient); multiple ports seems silly and implies the number of connnections is small enough to be dealt with by a single server port anyway.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    You will have to use a critical section to control access to the send function though, otherwise the messages may get garbled with one another. If you assign a particular thread to process a particular type of information then all incoming packets fo that type can be forwarded to teh appropriate thread, but if you have types of information that all threads can possibly have requested, then you need to have teh individual threads keep track of what information they requested and reject any pacets they didnt request, this adds overhead in that incoming packets have to be dispatched to each possible thread. I cant think off hand why you would need this complicated scheme.
    Last edited by abachler; 03-27-2009 at 12:13 PM.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    Why not give each request a unique tag (an integer). This tag gets passed back along with the response, where it can be dispatched to whichever thread allocated that tag. You could use the thread_id itself as the tag, probably.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with multiple threads
    By Cogman in forum Windows Programming
    Replies: 2
    Last Post: 07-05-2009, 09:40 AM
  2. Multiple Threads
    By NuNn in forum C Programming
    Replies: 3
    Last Post: 03-14-2009, 11:29 PM
  3. Question about Multiple threads and ring buffer.
    By qingxing2005 in forum C Programming
    Replies: 2
    Last Post: 01-15-2007, 12:30 AM
  4. Race condition: getting multiple threads to cooperate
    By FlyingDutchMan in forum C++ Programming
    Replies: 10
    Last Post: 03-31-2005, 05:53 AM
  5. Modeless Dialogs in Multiple threads
    By MrGrieves in forum Windows Programming
    Replies: 0
    Last Post: 06-22-2004, 01:33 PM