ThreadEnvironment.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_CXX11_THREADPOOL_THREAD_ENVIRONMENT_H
00011 #define EIGEN_CXX11_THREADPOOL_THREAD_ENVIRONMENT_H
00012 
00013 namespace Eigen {
00014 
00015 struct StlThreadEnvironment {
00016   struct Task {
00017     std::function<void()> f;
00018   };
00019 
00020   // EnvThread constructor must start the thread,
00021   // destructor must join the thread.
00022   class EnvThread {
00023    public:
00024     EnvThread(std::function<void()> f) : thr_(std::move(f)) {}
00025     ~EnvThread() { thr_.join(); }
00026 
00027    private:
00028     std::thread thr_;
00029   };
00030 
00031   EnvThread* CreateThread(std::function<void()> f) { return new EnvThread(std::move(f)); }
00032   Task CreateTask(std::function<void()> f) { return Task{std::move(f)}; }
00033   void ExecuteTask(const Task& t) { t.f(); }
00034 };
00035 
00036 }  // namespace Eigen
00037 
00038 #endif  // EIGEN_CXX11_THREADPOOL_THREAD_ENVIRONMENT_H
 All Classes Functions Variables Typedefs Enumerator