multiple threads read() problems

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default multiple threads read() problems

    Hello,

    I have a multi-threaded echo server designed like so:

    - there's a main threads and some number of worker threads
    (threadpool)
    - main thread periodically (every 200 ms) does a select() over client
    sockets
    - depending on select(), main thread either does accept() or sends a
    message
    to the worker thread pool (using POSIX message queues)
    - worker threads are in a loop waiting to mq_receive(); upon getting a
    message
    they read() from the client socket (which was indicated in the
    message) and write() it back exactly to the client socket

    All this is on Solaris 8 SPARC, using gcc with -lsocket -lpthread
    -lrt, and using plain vanilla SOCK_STREAM sockets.

    The behavior I am seeing on two clients is (imagine a timeline growing
    downward):

    client A client B
    connects connects
    sends message1 sends message1
    gets echo
    sends message2 sends message2
    gets echo
    disconnects
    gets echo of strcat(message1, message2)

    In other words, the second client does not get *any* response back
    until the first client has disconnected. Needless to say, this sucks.
    I am looking for answers/explanations for the following questions:

    1. Why does the second client have this "hysteresis" effect?
    2. Is there a better way - than mq_xxx - to send messages to a
    threadpool?
    3. All of the sockets (listening socket, client sockets) are plain
    vanilla SOCK_STREAM sockets. Should I be setting some flags on them?
    4. Should the worker threads read() or write() differently, e.g. with
    flags or other use some other function?
    5. The number of threads in the threadpool is arbitrary. It's usually
    5-10. Will it go faster if there are more threads? Is there a
    threshold?
    6. What's a good way to debug multi-threaded processes? (I'm using
    lots and lots of printfs.) Is there a debugger that can pinpoint the
    timeline more or less accurately?

    Thanks in advance!

    John
    John Galt Guest

  2. Similar Questions and Discussions

    1. Action Script to read multiple datasets in XML doc forFlash Bar Chart
      I am trying to compare groups of items using an XML source and need assistance in creating the Action Script to properly read multiple datasets...
    2. to read multiple cfreturn
      Hi All, I'm just exploring the CF component function. I've face oe problem which is to read more than1 cfreturn. Here the code and file name : ...
    3. cf7 installation problems?? read this !!
      after installing and uninstalling cf7 4x and not even get passed the setup of my browser IE (security problems) i have tried absolutly everything...
    4. Blocking on multiple threads with timeout
      I have a few threads that might need as long as a minute or more to complete and terminate. If they exceed an arbitrary time, they can be...
    5. Using a trace function with multiple threads
      Hello all, I'd like to know if the set_trace_func function works on a "per thread" basis or if it works globally. I have a multithreaded...
  3. #2

    Default Re: multiple threads read() problems


    "John Galt" <johngalt__@hotmail.com> wrote in message
    news:216f064.0307080120.145b7f67@posting.google.co m...
    > 1. Why does the second client have this "hysteresis" effect?
    No idea, it's probably a bug in your code.
    > 2. Is there a better way - than mq_xxx - to send messages to a
    > threadpool?
    Since threads share all their memory, you should just put it in a queue
    that the threads check. Use a mutex and condition variable to protect the
    queue and give the workers something to block on.
    > 3. All of the sockets (listening socket, client sockets) are plain
    > vanilla SOCK_STREAM sockets. Should I be setting some flags on them?
    Just make them non-blocking.
    > 4. Should the worker threads read() or write() differently, e.g. with
    > flags or other use some other function?
    Nope.
    > 5. The number of threads in the threadpool is arbitrary. It's usually
    > 5-10. Will it go faster if there are more threads? Is there a
    > threshold?
    No, 5-10 is definitely enough if they don't block.
    > 6. What's a good way to debug multi-threaded processes? (I'm using
    > lots and lots of printfs.) Is there a debugger that can pinpoint the
    > timeline more or less accurately?
    It depends on your platform. Purify is pretty good for some types of
    bugs.

    DS


    David Schwartz Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139