multiple threads read() problems

multiple threads read() problems

Post by John Ga » Wed, 09 Jul 2003 18:20:34



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 de* that can pinpoint the
timeline more or less accurately?

Thanks in advance!

John

 
 
 

multiple threads read() problems

Post by David Schwart » Thu, 10 Jul 2003 03:04:59



Quote:> 1. Why does the second client have this "hysteresis" effect?

    No idea, it's probably a bug in your code.

Quote:> 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.

Quote:> 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.

Quote:> 4. Should the worker threads read() or write() differently, e.g. with
> flags or other use some other function?

    Nope.

Quote:> 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.

Quote:> 6. What's a good way to debug multi-threaded processes? (I'm using
> lots and lots of printfs.) Is there a de* that can pinpoint the
> timeline more or less accurately?

    It depends on your platform. Purify is pretty good for some types of
bugs.

    DS

 
 
 

1. multiple thread 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

2. making a login loging script

3. How can I multiple read() through multiple threads?

4. Port of midiplay

5. Multiple r/w, Multiple read?

6. LILO woes

7. multiple threads and Xlib collision? async access problem

8. Magic 6.4.4 for Linux uploaded to sunsite.unc.edu

9. Threads, threads, threads

10. Threads in linux versus threads in NT and threads in Solaris.

11. threads packages: kernel threads vs. user threads

12. POSIX threads, thread-specific data: what about the "main" thread?