简体   繁体   中英

Thread pool and execution queue in c++11

I have a bunch of parallel tasks to complete, but only a few worker threads (say 8, but I want this to be configurable).

So, 8 threads run, and each of the threads pops the next task from the queue, as long as the queue has tasks.

Does C++11 provide any inbuilt constructs to help implement this design?

I see some similar discussions related to std::async , but I think it leaves too much to the implementation of the compiler.

You can have std::vector<std::thread> if you want but the pool and work queue you have to implement yourself in C++11.

If you want generic thread pool implementation then Boost.Asio contains one. You simply have to io_service::run() from several threads to set up a pool of threads and then the works to pool can be given with io_service::post() . Quite clean and simple and generic only that name io_service is confusing if the works what you do are not I/O related.

If you mean a threading-pool, no C++11 doesn't provide one, you also have to decide if you want to use atomics, mutexes or fences in your threading model with C++11, if you are looking for something that will work out of the box the only real solution, AFAIK, is the Intel TBB library. There is an unofficial boost threading pool library too but doesn't look really popular or active.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM