- The thread pool has a fixed size buffer to store all the the tasks to be executed, if the buffer is full, the thread will wait until there is available space for the new task
- The worker threads will actively remove the task from the queue and execute them, if there is no available task, it will wait until it is notified there is new task added
The general logic is pretty similar to producer consumer queue problem we discussed about earlier. If you have problem to understand the code below, please check the article above:
Some explanations:
- The task will be a base class and if you want to do something different in task, you can inherit it and reimplement the run function
- C++ thread will start after it is constructed, so don't be confused if you don't see something like thread.start()
- There will be multiple thread to add tasks to thread pool, all threads in pool will keep executing tasks. All this process is coordinated by lock and condition variables to make sure it behaves as expected, for more details, please check the article above
Example output:
No comments:
Post a Comment