Boost::thread_pool wait for available thread to post new task

Viewed 25

I am new to boost::thread_pool. I can create thread pool with 4 threads. But how many tasks can I post? More that 4. Let say 100. Does that mean that 96 will wait while first 4 will be processed?

From documentation there is only join method which waits when all threads will be done. There is no method to check if there is only one thread available to post any data. I would like to wait until there is at least one available thread exists to post a new task. Is that possible?

1 Answers

Assuming you meant boost::asio::thread_pool, then yes, if you post 100 tasks to a pool of 4 it means that they will all be executed in turn on the next available thread, on average about 25 tasks per thread, assuming they all have similar execution times.

This is the nature of a thread pool. If you want to limit the amount of pending tasks, use a bounded queue. You could have a different queue capacity than the number of threads in the pool.

Related