views:

63

answers:

2

I'm creating a server-type application at the moment which will do the usual listening for connections from external clients and, when they connect, handle requests, etc.

At the moment, my implementation creates a pair of threads every time a client connects. One thread simply reads requests from the socket and adds them to a queue, and the second reads the requests from the queue and processes them.

I'm basically looking for opinions on whether or not you think having all of these threads is overkill, and importantly whether this approach is going to cause me problems.

It is important to note that most of the time these threads will be idle - I use wait handles (ManualResetEvent) in both threads. The Reader thread waits until a message is available and if so, reads it and dumps it in a queue for the Process thread. The Process thread waits until the reader signals that a message is in the queue (again, using a wait handle). Unless a particular client is really hammering the server, these threads will be sat waiting. Is this costly?

I'm done a bit of testing - had 1,000 clients connected continually nagging - the server (so, 2,000+ threads) and it seemed to cope quite well.

A: 

I advice to use following patter. First you need thread pool - build in or custom. Have a thread that checks is there something available to read, if yes it picks Reader thread. Then reading thread puts into queue and then thread from pool of processing threads will pick it. it will minimize number of threads and minimize time spend in waiting state

Andrey
+1  A: 

I think your implementation is flawed. This kind of design doesn't scale because creating threads is expensive and there is a limit on how many threads can be created. That is the reason that most implementations of this type use a thread pool. That makes it easy to put a cap on the maximum amount of threads while easily managing new connections and reusing the threads when the work is finished.

If all you are doing with your thread is putting items in a queue, then use the ThreadPool.QueueUserWorkItem method to use the default .NET thread pool.

You haven't given enough information in your question to specify for definite but perhaps you now only need one other thread, constantly running clearing down the queue, you can use a wait handle to signal when something has been added.

Just make sure to synchronise access to your queue or things will go horribly wrong.

Phill
I may be able to utilise the thread pool for handling the processing of messages. I wonder if it would be worth having a thread that reads from a number of clients sequentially (not necessarily all of them - I could have a thread per hundred clients, for example). This would means much fewer threads, but I might potentially end up with one client slowing the others down - something I really want to avoid.Thanks for your reply.
Barguast
It's hard to say with no details on the nature of the connection. Is it a long lived connection with a constant stream of data being read from the client? Or is the data sent sporadically or in one short message from the client?
Phill