简体   繁体   English

ThreadPool用OpenMP实现

[英]ThreadPool implemented with OpenMP

I need to implement a thread pool executor for generic tasks. 我需要为通用任务实现一个线程池执行器。 My idea is to use OpenMP for thread management. 我的想法是使用OpenMP进行线程管理。 The problem is that I'm not familar yet with OpenMP.. 问题是我对OpenMP还不熟悉。

I tried to find an existing implementation of the generic ThreadPool with OpenMP, but I haven't find one so far. 我试图用OpenMP找到通用ThreadPool的现有实现,但到目前为止我还没找到。 What I would like to have in the end is something very similar to java.util.concurrent.ThreadPoolExecutor : 我最终想要的是与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
        }
    }
};

I have several questions about this approach: 我有几个关于这种方法的问题:

  1. Is there existing implementation of thread pool with OpenMP for C++? 是否存在使用OpenMP for C ++的线程池实现? [I know I can implement threadpool with boost::asio::io_service, but I would prefer not depend on it] [我知道我可以用boost :: asio :: io_service实现threadpool,但我宁愿不依赖它]

  2. With my design - how can we guarantee that the OpenMP threads will be 'owned' by the concrete ThreadPoolExecutor instance? 通过我的设计 - 我们如何保证OpenMP线程将由具体的ThreadPoolExecutor实例“拥有”? They should not be static for all instances. 对于所有实例,它们不应该是静态的。

Thank you for any advice & constructive critics of this approach & proposals of other implementations. 感谢您对此方法的任何建议和建设性批评以及其他实现的建议。

Just to sum up: as described in the comments OpenMP is not an option for implementing generic thread pool or executor, because: 总结一下:如评论中所述,OpenMP不是实现通用线程池或执行器的选项,因为:

  1. OpenMP is strictly a fork/join threading model. OpenMP严格来说是一个fork / join线程模型。
  2. You can't assign the owner for OpenMP threads 您无法为OpenMP线程分配所有者
  3. No control over threads and internal threadpool 无法控制线程和内部线程池

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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