clinuxmultithreadingserverposix-select

When should I use select comparing with multithreading?


I just start to learn to code for Linux server.
After coding a simple server with multithreading, I don't know when we should use select.

My server with multithreading is very simple:

while(true)
{
    client = accept(sock);
    pthread_create(client, processClientFunc);
}

Each client has its own thread so it can communicate with the server.

How I've heard that there were three functions: select, poll and epoll.

Google told me that select could monitor multiple file descriptors.
Well, OK, I understand but I think the multithreading can cover all of needs. Also, I think I still need to use multithreading even if I use select.

So my question is: when should we use select.


Solution

  • I've never used select myself, as poll provides better scalability if I recall correctly. I use poll when I'm handling a lot of connections and I'm trying to minimize the number of threads used. Multithreading is expensive and generally unnecessary unless you have a lot of connections or your connections are computationally/blocking expensive. The use of a well placed poll or select will allow you to handle them all in one thread, even accepting the sockets and handling the clients too (at least with poll).

    So use poll/select whenever you have many connections that are inexpensive to save on multithreaded overhead.