If your http request processing is time-consuming, then it is a good choice to create a thread for each http request. But your current situation is that the time to process the task is very short, so most of the time is spent on creating and destroying threads.
You can use the thread pool to process the task queue. You can refer to the implementation below. C++11 simple thread pool code reading http://www.cnblogs.com/oloroso/p/5881863.html
The overhead of thread creation and destruction is very high, and if there are too many threads, the memory usage will be very high. It is estimated that under the fourth generation i7 DDR5, only a thousand levels of creation and destruction can be completed per second. This overhead is configuration and platform dependent.
The thread pool implemented using std::condition_variable and std::mutex with the same configuration can easily complete millions of synchronizations per second.
You can write a simple program to test the specific time consumption. It is recommended to use a thread pool.
In order to prevent sudden requests, a thread pool needs to be used to allocate the number of threads in advance according to business requests. The operation of the thread pool is asynchronous, which greatly reduces the competition for thread resources.
If your http request processing is time-consuming, then it is a good choice to create a thread for each http request.
But your current situation is that the time to process the task is very short, so most of the time is spent on creating and destroying threads.
You can use the thread pool to process the task queue.
You can refer to the implementation below.
C++11 simple thread pool code reading http://www.cnblogs.com/oloroso/p/5881863.html
The overhead of thread creation and destruction is very high, and if there are too many threads, the memory usage will be very high. It is estimated that under the fourth generation i7 DDR5, only a thousand levels of creation and destruction can be completed per second. This overhead is configuration and platform dependent.
The thread pool implemented using std::condition_variable and std::mutex with the same configuration can easily complete millions of synchronizations per second.
You can write a simple program to test the specific time consumption. It is recommended to use a thread pool.
In order to prevent sudden requests, a thread pool needs to be used to allocate the number of threads in advance according to business requests. The operation of the thread pool is asynchronous, which greatly reduces the competition for thread resources.