views:

168

answers:

3

I have an application processing network communication with blocking calls. Each thread manages a single connection. I've added a timeout on the read and write operation by using select prior to read or write on the socket.

Select is known to be inefficient when dealing with large number of sockets. But is it ok, in term of performance to use it with a single socket or are there more efficient methods to add timeout support on single sockets calls ? The benefit of select is to be portable.

+2  A: 

Yes that's no problem, and you do want some timeout mechanisms to not leak resources from bad behaving clients etc.

Note that having a large number of threads is even more inefficient than having select dealing with a large number of sockets.

nos
Thanx. I plan to use libev to handle multiple concurrent communication channels. At this stage I simply consider one thread accessing one socket.
chmike
+2  A: 

If yo uthink select is inefficient with a large number of sockets, try handling a large number of sockets with one thread per sockt. You are in for a world of pain. Like you will have problems scaling to 1000 threads.

What I have done in the past is that:

  • Group sockets in groups of X (512, 1024).
  • Have one thread or two run along those groups and select () - then hand off the sockets with new data into a queue.
  • have a number of worker threads work off those sockets with new data. How many depends how much I need to max out the CPU ;)

This way I dont ahve a super über select () with TONS of items, and I also dont waste ridiculous amountf of memory on threads (hint: every thread needs it's own stack. With ONLY 2mb, that is 2gb for 1000 sockets - talk about inefficient) and waste hugh amounts of CPU on doing useless context switches.

TomTom
A: 

The question with threads/select is whether you want to avoid clients blocking each other. If this is not an issue, then work single-threaded. If it is, choose an appropriate threading scheme (1 thread per connection, worker threads per connection, worker thread per request,...).

When working with 1 thread per connection, a select per read/write is a decent solution, but generally speaking, it is better to work with non-blocking sockets in combination with select to avoid blocking in situations where only a part of the expected message arrives and then do a select after writing.

stefaanv