1 //===-- llvm/Support/ThreadPool.h - A ThreadPool implementation -*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines a crude C++11 based thread pool.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_SUPPORT_THREAD_POOL_H
14 #define LLVM_SUPPORT_THREAD_POOL_H
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/Support/thread.h"
22 #include <condition_variable>
31 /// A ThreadPool for asynchronous parallel execution on a defined number of
34 /// The pool keeps a vector of threads alive, waiting on a condition variable
35 /// for some work to become available.
38 using TaskTy
= std::function
<void()>;
39 using PackagedTaskTy
= std::packaged_task
<void()>;
41 /// Construct a pool with the number of threads found by
42 /// hardware_concurrency().
45 /// Construct a pool of \p ThreadCount threads
46 ThreadPool(unsigned ThreadCount
);
48 /// Blocking destructor: the pool will wait for all the threads to complete.
51 /// Asynchronous submission of a task to the pool. The returned future can be
52 /// used to wait for the task to finish and is *non-blocking* on destruction.
53 template <typename Function
, typename
... Args
>
54 inline std::shared_future
<void> async(Function
&&F
, Args
&&... ArgList
) {
56 std::bind(std::forward
<Function
>(F
), std::forward
<Args
>(ArgList
)...);
57 return asyncImpl(std::move(Task
));
60 /// Asynchronous submission of a task to the pool. The returned future can be
61 /// used to wait for the task to finish and is *non-blocking* on destruction.
62 template <typename Function
>
63 inline std::shared_future
<void> async(Function
&&F
) {
64 return asyncImpl(std::forward
<Function
>(F
));
67 /// Blocking wait for all the threads to complete and the queue to be empty.
68 /// It is an error to try to add new tasks while blocking on this call.
72 /// Asynchronous submission of a task to the pool. The returned future can be
73 /// used to wait for the task to finish and is *non-blocking* on destruction.
74 std::shared_future
<void> asyncImpl(TaskTy F
);
77 std::vector
<llvm::thread
> Threads
;
79 /// Tasks waiting for execution in the pool.
80 std::queue
<PackagedTaskTy
> Tasks
;
82 /// Locking and signaling for accessing the Tasks queue.
84 std::condition_variable QueueCondition
;
86 /// Locking and signaling for job completion
87 std::mutex CompletionLock
;
88 std::condition_variable CompletionCondition
;
90 /// Keep track of the number of thread actually busy
91 std::atomic
<unsigned> ActiveThreads
;
93 #if LLVM_ENABLE_THREADS // avoids warning for unused variable
94 /// Signal for the destruction of the pool, asking thread to exit.
100 #endif // LLVM_SUPPORT_THREAD_POOL_H