Thread pool and execution queue in c++11

最后都变了- 提交于 2019-12-11 01:45:32

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/17961899/thread-pool-and-execution-queue-in-c11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!