nog*_*ard 5 c++ concurrency multithreading openmp threadpool
我需要为一般任务实现一个线程池执行程序。我的想法是使用OpenMP进行线程管理。问题是我还不熟悉OpenMP。
我试图用OpenMP找到通用ThreadPool的现有实现,但到目前为止还没有找到。最后我想拥有的东西非常类似于java.util.concurrent.ThreadPoolExecutor
:
template <typename Return, typename Task>
class ThreadPoolExecutor
{
public:
ThreadPoolExecutor(int threadCount);
// asyncronous invoke
boost::unique_future<Return> submit(const TaskPtr & task);
// blocking call
std::vector<Return> invokeAll(const std::vector<TaskPtr> & tasks)
{
// submit all tasks + wait for completion
#pragma omp parallel default(shared)
{
// call tasks here
}
}
};
Run Code Online (Sandbox Code Playgroud)
我对此方法有几个疑问:
OpenMP for C ++是否存在线程池的现有实现?[我知道我可以使用boost :: asio :: io_service来实现线程池,但是我不想依赖它。]
通过我的设计-我们如何保证具体的ThreadPoolExecutor实例将“拥有” OpenMP线程?在所有情况下,它们都不应该是静态的。
感谢您对这种方法和其他实现的建议和建设性批评。
总结一下:正如评论中所述,OpenMP 不是实现通用线程池或执行器的选项,因为: