Eigen  5.0.1
 
Loading...
Searching...
No Matches
ThreadEnvironment.h
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_THREADPOOL_THREAD_ENVIRONMENT_H
12#define EIGEN_THREADPOOL_THREAD_ENVIRONMENT_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19struct StlThreadEnvironment {
20 struct Task {
21 std::function<void()> f;
22 };
23
24 // EnvThread constructor must start the thread,
25 // destructor must join the thread.
26 class EnvThread {
27 public:
28 EnvThread(std::function<void()> f) : thr_(std::move(f)) {}
29 ~EnvThread() { thr_.join(); }
30 // This function is called when the threadpool is cancelled.
31 void OnCancel() {}
32
33 private:
34 std::thread thr_;
35 };
36
37 EnvThread* CreateThread(std::function<void()> f) { return new EnvThread(std::move(f)); }
38 Task CreateTask(std::function<void()> f) { return Task{std::move(f)}; }
39 void ExecuteTask(const Task& t) { t.f(); }
40};
41
42} // namespace Eigen
43
44#endif // EIGEN_THREADPOOL_THREAD_ENVIRONMENT_H