简体   繁体   中英

ThreadPool implemented with OpenMP

I need to implement a thread pool executor for generic tasks. My idea is to use OpenMP for thread management. The problem is that I'm not familar yet with OpenMP..

I tried to find an existing implementation of the generic ThreadPool with OpenMP, but I haven't find one so far. What I would like to have in the end is something very similar to 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++? [I know I can implement threadpool with boost::asio::io_service, but I would prefer not depend on it]

  2. With my design - how can we guarantee that the OpenMP threads will be 'owned' by the concrete ThreadPoolExecutor instance? 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:

  1. OpenMP is strictly a fork/join threading model.
  2. You can't assign the owner for OpenMP threads
  3. No control over threads and internal threadpool

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