10 #ifndef EIGEN_CXX11_THREADPOOL_SIMPLE_THREAD_POOL_H 11 #define EIGEN_CXX11_THREADPOOL_SIMPLE_THREAD_POOL_H 20 template <
typename Environment>
21 class SimpleThreadPoolTempl :
public ThreadPoolInterface {
24 explicit SimpleThreadPoolTempl(
int num_threads, Environment env = Environment())
25 : env_(env), threads_(num_threads), waiters_(num_threads) {
26 for (
int i = 0; i < num_threads; i++) {
27 threads_.push_back(env.CreateThread([
this, i]() { WorkerLoop(i); }));
33 ~SimpleThreadPoolTempl() {
36 std::unique_lock<std::mutex> l(mu_);
37 while (!pending_.empty()) {
43 for (
auto w : waiters_) {
51 for (
auto t : threads_) {
58 void Schedule(std::function<
void()> fn)
final {
59 Task t = env_.CreateTask(std::move(fn));
60 std::unique_lock<std::mutex> l(mu_);
61 if (waiters_.empty()) {
62 pending_.push_back(std::move(t));
64 Waiter* w = waiters_.back();
67 w->task = std::move(t);
72 int NumThreads() const final {
73 return static_cast<int>(threads_.size());
76 int CurrentThreadId() const final {
77 const PerThread* pt = this->GetPerThread();
78 if (pt->pool ==
this) {
86 void WorkerLoop(
int thread_id) {
87 std::unique_lock<std::mutex> l(mu_);
88 PerThread* pt = GetPerThread();
90 pt->thread_id = thread_id;
94 if (pending_.empty()) {
97 waiters_.push_back(&w);
105 t = std::move(pending_.front());
106 pending_.pop_front();
107 if (pending_.empty()) {
121 typedef typename Environment::Task Task;
122 typedef typename Environment::EnvThread Thread;
125 std::condition_variable cv;
131 constexpr PerThread() : pool(NULL), thread_id(-1) { }
132 SimpleThreadPoolTempl* pool;
138 MaxSizeVector<Thread*> threads_;
139 MaxSizeVector<Waiter*> waiters_;
140 std::deque<Task> pending_;
141 std::condition_variable empty_;
142 bool exiting_ =
false;
144 PerThread* GetPerThread()
const {
145 EIGEN_THREAD_LOCAL PerThread per_thread;
150 typedef SimpleThreadPoolTempl<StlThreadEnvironment> SimpleThreadPool;
154 #endif // EIGEN_CXX11_THREADPOOL_SIMPLE_THREAD_POOL_H Namespace containing all symbols from the Eigen library.
Definition: AdolcForward:45