Fog Creek Software
Discussion Board

handle connections -select or multithreading?

Which is better to use for multiple client? single thread for each client, or something like select() in UNIX?

Thursday, February 6, 2003

If you have lots of clients (hundreds or thousands) then select() (or some form of non-blocking i/o) is necessary.  If you have lots of clients that do just a little i/o then a pool or worker threads can work well.  Otherwise use sock per channel.

If you're app is not already multi-threaded then I would avoid it like the plague.  Single threaded apps are much easier to develop and debug.

Thursday, February 6, 2003

Use select() or WaitForMultipleObjects().

Thursday, February 6, 2003

It truly depends on what you're trying to accomplish.  Are you writing a massive server with lots of connect requests - and possibly short socket lifetimes (like http)?

Or, perhaps you're writing a streaming server or client which is going to send lots of multicast data over some longer period of time.

Take a look at this Apache document, which will describe the problems of accept() and select() along with some interesting paths for solutions you might chose to investigate.

Are you doing something massive?  If so, then the question is not just should I use select() as a demultiplexer vs. threads and no multiplexer.  You should look into IO methods and benchmark overheads with respect ot what you want to do.

If its not massive, then just do whatever makes life simple and maintainable.  That too will depend upon you application.

Either way, there is no single answer.

Nat Ersoz
Thursday, February 6, 2003

There are two basic ways to handle multiple connections in the server. You don't mention what OS you're using - I'll assume windows at the moment.

The select() style is known as the Reactor Pattern. You're familiar with the basic idea here.

The other choice is async I/O which is generally better under windows (select() is implemented fairly poorly on Win32). This is the Proactor pattern.

There are writeups on both of these styles with compare/contrast at:

Once you get past the theoretical stuff, on Win32 the best way to do high performance servers with many clients is to use I/O completion ports. A completion port is basically a thread pool manager built into the OS specifically for managing I/O requests.

Under Unix, select is pretty much your only option. There are async I/O operations available on some unices, but they all work slightly differently.

Chris Tavares
Thursday, February 6, 2003

Whoops, forget the URL  ^^^

Nat Ersoz
Thursday, February 6, 2003

Why is it an either-or question? Consider using the ACE_TP_Reactor from the ACE library. It's not the same as the thread-per-connection model, but can still enjoy some of the same responsiveness.

Steven E. Harris
Thursday, February 6, 2003

There's a good discussion of this subject here:

Thursday, February 6, 2003

does anybody know how EJB's handle multiple clients internally? does it use one client per thread model?

Thursday, February 6, 2003

*  Recent Topics

*  Fog Creek Home