Skip to content
Advertisement

epoll: must I use multi-threading

I’ve got a basic knowledge from here about epoll. I know that epoll can monitor multiple FDs and handle them.

My question is: can a heavy event block the server so I must use multithreading?

For example, the epoll of a server is monitoring 2 sockets A and B. Now A starts to send lot of messages to the server so the server starts to read them. One second later, B starts to send messages too while A is still sending. In this case, Need I create a thread for these read actions? If I don’t, does it mean that the server has no chance to get the messages from B until A finishes its sending?

Advertisement

Answer

If you can process incoming messages fast enough (no blocking calls, no heavy computations), you don’t need a separate thread. Otherwise, you would benefit from going multi-threaded.

In any case, it helps to understand what happens when you have only one thread and you can’t process messages fast enough. If you are working with TCP protocol, the machines sending you the data will simply reduce their transmission rate. When using UDP, some incoming packets will get dropped.

Advertisement