What is better: Select vs Threads? What is better: Select vs Threads? multithreading multithreading

What is better: Select vs Threads?


Using select is better for performance, especially when you could have potentially hundreds of simultaneous operations. However it can be difficult to write the code correctly and the style of coding is very different from traditional single threaded programming. For example, you need to avoid calling any blocking methods as it could block your entire application.

Most people find using threads simpler because the majority of the code resembles ordinary single threaded code. The only difficult part is in the few places where you need interthread communication, via mutexes or other synchronization mechanisms.

In your specific case it seems that you will only need a small number of threads, so I'd go for the simpler programming model using threads.


Given the amount of work you're doing, it probably doesn't matter.

For high performance applications, there is a difference. In these cases, you need to be handling several thousand connections simultaneously; in such cases, you hand off new connections to new threads.

Creating several thousand threads is expensive, so selecting is used for efficiency. Actually different techniques such as kqueue or epoll are used for optimal switching.

I say it doesn't matter, because you're likely only going to create the thread once and have exactly two threads running for the lifetime of the application.