30dayMakeCppServer icon indicating copy to clipboard operation
30dayMakeCppServer copied to clipboard

What is the point of ThreadPool?

Open philipzhux opened this issue 2 years ago • 2 comments

On Day 10 when the thread pool was first introduced, it made sense as we were pushing individual handler functions into the task queue (so thread pool achieves dynamic allocation of tasks). However, from Day 11 on, only EventLoop::loop() would be pushed into the queue, and EventLoop::loop() takes care of a (sub/main)-reactor and never returns in normal case (fixed therefore once allocated). As the number of threads is the same as the number of EventLoop objects, the effect is basically a one-to-one allocation of EventLoops to threads. With such being the case, why not just assign an EventLoop per thread?

philipzhux avatar Sep 09 '22 14:09 philipzhux

Didn't get your point about the one-to-one allocation of EventLoops to threads. However for question of the last sentence: Indeed, the current code does assign an EventLoop per thread. Each sub-reactor loops in one thread, handling read/write event of connections.

yuesong-feng avatar Nov 04 '22 15:11 yuesong-feng

Let me make it clearer. According to my understanding, the point of a thread pool is to dispatch task dynamically to threads. Now that we have fixed number of thread and an EventLoop per thread (the lifecycles of EventLoop and thread are bound together), we can simply run the EventLoop when creating the thread. So, it does not really utilize the thread pool.

philipzhux avatar Nov 11 '22 11:11 philipzhux