ThreadEnvironment.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CXX11_THREADPOOL_THREAD_ENVIRONMENT_H
11 #define EIGEN_CXX11_THREADPOOL_THREAD_ENVIRONMENT_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
18  struct Task {
19  std::function<void()> f;
20  };
21 
22  // EnvThread constructor must start the thread,
23  // destructor must join the thread.
24  class EnvThread {
25  public:
26  EnvThread(std::function<void()> f) : thr_(std::move(f)) {}
27  ~EnvThread() { thr_.join(); }
28  // This function is called when the threadpool is cancelled.
29  void OnCancel() { }
30 
31  private:
32  std::thread thr_;
33  };
34 
35  EnvThread* CreateThread(std::function<void()> f) { return new EnvThread(std::move(f)); }
36  Task CreateTask(std::function<void()> f) { return Task{std::move(f)}; }
37  void ExecuteTask(const Task& t) { t.f(); }
38 };
39 
40 } // namespace Eigen
41 
42 #endif // EIGEN_CXX11_THREADPOOL_THREAD_ENVIRONMENT_H
: InteropHeaders
Definition: Core:139
Definition: BFloat16.h:222
Task CreateTask(std::function< void()> f)
void ExecuteTask(const Task &t)
EnvThread * CreateThread(std::function< void()> f)