Multi-Threaded Sockets

This is a discussion on Multi-Threaded Sockets within the Networking/Device Communication forums, part of the General Programming Boards category; Is it possible to have the same socket spread over two theads? IE having one thread doing a recv( ) ...

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Multi-Threaded Sockets

    Is it possible to have the same socket spread over two theads? IE having one thread doing a recv( ) on the socket, whilst the other one does a send( )? I looked into select( ), but I want something less CPU-intensive.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Well, in a way, that really doesn't make much sense to do that way. After all, the request-reply cycle is pretty much like wheels turning, that is, they should follow eachother. Of course, there may be times where this would be desirable. Is it safe, though? I'm not so sure. It seems a little dangerous. Imagine doing reading/writing from the same file in separate threads. Kind of sketchy business! Probably a more kosher thing to do would be to launch each socket into it's own thread, meaning you could serve multiple sockets while still maintaining a sense syncronicity by reading/writing from the same thread.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What I'm doing is a client-server chat program. I don't want the server to wait for the client to send data before it can send information to the client.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    select() is not cpu intensive. i wrote a chat server (im making usernames and whatnot improvements now) that uses select() and cpu usage stays at 0%.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You used it in an infinite loop?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    You may want to see Is Winsock thread-safe? from the Winsock Programmer's FAQ for an answer to your original question.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Guess I'll just stick with select then, since send( ) and recv( ) on the same socket simultaneously is a grey area.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    yes i used it in an infinite loop.

    if you notice the processor usage goes up, then you made a mistake somewhere. it should work unnoticably.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    The point of select is so you do not have to do constant polling on the socket, not to mention you can check a whole set of sockets at once instead of one at a time. It is not CPU intensive, assuming you use it correctly. Threads are rarley the answer to the question, I would say. You would need to set up some sort of concurrency if you wanted one socket on multiple threads, so you would not gain anything from multithreading your application.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    I totally disagree, orbitz. There really isn't much advantage to running sockets from the main thread, since that ties up the user interface too much, even using select(). From that standpoint, *at least* one thread is absolutely necessary, and especially so (obviously) if you are using blocking sockets. My weapons of choice would be blocking sockets launched into individual threads.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sockets and multi string comparison
    By davo666 in forum C Programming
    Replies: 3
    Last Post: 03-17-2009, 07:19 AM
  2. multi threaded program
    By Dash_Riprock in forum C++ Programming
    Replies: 6
    Last Post: 08-27-2006, 08:38 AM
  3. Sockets, multi home, multicast, security, complicated issue
    By ekymk in forum Networking/Device Communication
    Replies: 6
    Last Post: 08-13-2004, 02:12 AM
  4. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 10:49 AM
  5. Signals in multi threaded application
    By HelpMe in forum C Programming
    Replies: 0
    Last Post: 09-12-2001, 09:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21