c ++ 11中的线程池和执行队列

时间:2022-07-16 20:59:10

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

我有一堆并行任务要完成,但只有少数工作线程(比如8,但我希望这是可配置的)。

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

因此,只要队列具有任务,就会运行8个线程,并且每个线程从队列中弹出下一个任务。

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

C ++ 11是否提供任何内置构造来帮助实现此设计?

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

我看到一些与std :: async相关的类似讨论,但我认为它对编译器的实现留下了太多的余地。

2 个解决方案

#1


2  

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

如果需要,可以使用std :: vector ,但必须在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.

如果您想要通用线程池实现,那么Boost.Asio包含一个。你只需要从几个线程的io_service :: run()来建立一个线程池,然后可以用io_service :: post()给出池的工作。非常干净,简单和通用只有名称io_service令人困惑,如果你做的工作不是I / O相关。

#2


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.

如果你的意思是一个线程池,没有C ++ 11没有提供一个,你还必须决定是否要在你的线程模型中使用原子,互斥或围栏与C ++ 11,如果你正在寻找一些东西开箱即用的唯一真正的解决方案AFAIK是英特尔TBB库。还有一个非官方的提升线程池库,但看起来不是很受欢迎或活跃。

#1


2  

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

如果需要,可以使用std :: vector ,但必须在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.

如果您想要通用线程池实现,那么Boost.Asio包含一个。你只需要从几个线程的io_service :: run()来建立一个线程池,然后可以用io_service :: post()给出池的工作。非常干净,简单和通用只有名称io_service令人困惑,如果你做的工作不是I / O相关。

#2


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.

如果你的意思是一个线程池,没有C ++ 11没有提供一个,你还必须决定是否要在你的线程模型中使用原子,互斥或围栏与C ++ 11,如果你正在寻找一些东西开箱即用的唯一真正的解决方案AFAIK是英特尔TBB库。还有一个非官方的提升线程池库,但看起来不是很受欢迎或活跃。