'How to build a high concurrent tcp server with a small number of threads

My design is as follows:

Make the receiving socket non-blocking. Set epoll_wait to timeout 0. Loop in the main thread to receive clients and judge epoll_wait. If an event occurs, create a thread to process I/O operation。

I think this scheme has disadvantages:

  1. Too many threads are opened. Although it can be managed by a thread pool, it seems that a large number of threads need to be opened to handle concurrency.
  2. Use queues to manage read and write tasks, Only a small number of threads are required to process queued tasks. But this may cause server delays.

I have an idea: The task is handed over to the linux kernel through aio_read and aio_write. This seems to reduce the number of server threads, but I'm not sure if this is potentially risky.

My unused knowledge: No multi-process programming is used in the server.

Thanks to everyone who suggested.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source