ThreadPool.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarX
19  * @author Mirko Waechter( mirko.waechter at kit dot edu)
20  * @date 2018
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include <future>
27 #include <mutex>
28 
29 namespace armarx
30 {
31 
32  /**
33  * @brief The ThreadPool class
34  *
35  * Usage:
36  * \code{.cpp}
37  * ThreadPool pool(5);
38  * auto handle = pool.runTask([]{
39  * ...
40  * });
41  * handle.join();
42  * // or handle.detach();
43  * \endcode
44  */
45  class ThreadPool
46  {
47  private:
48  struct Impl;
49  std::unique_ptr<Impl> impl;
50 
51  public:
52  class Handle
53  {
54  public:
55  Handle();
56  Handle(Handle&&) = default;
57  Handle(std::shared_future<void> functionFinished);
58  Handle& operator=(Handle&&) = default;
59  ~Handle() noexcept(false);
60 
61  operator bool() const
62  {
63  return isValid();
64  }
65 
66  bool isValid() const;
67  void join();
68  void detach();
69  const std::shared_future<void>& getFuture() const;
70 
71  bool isDetached() const;
72  bool isJoined() const;
73 
74  private:
75  std::shared_ptr<std::mutex> mutex;
76  std::shared_future<void> functionFinished;
77  bool joined = false;
78  bool detached = false;
79  };
80 
81  /// @brief Constructor.
82  ThreadPool(std::size_t pool_size, bool queueTasks = false);
83 
84  /// @brief Destructor.
85  ~ThreadPool();
86 
87  /// @brief Adds a task to the thread pool if a thread is currently available.
88  /// @return returns true if a thread is available, otherwise returns false.
89  Handle runTask(std::function<void()> task);
90 
91  /**
92  * @brief If queing is disabled, returns the number of available threads. Otherwise returns -1;
93  */
94  int getAvailableTaskCount() const;
95 
96  private:
97  /// @brief Wrap a task so that the available count can be increased once
98  /// the user provided task has completed.
99  void wrap_task(std::function<void()> task);
100  };
101 
102 
103 } // namespace armarx
armarx::ThreadPool::ThreadPool
ThreadPool(std::size_t pool_size, bool queueTasks=false)
Constructor.
Definition: ThreadPool.cpp:56
armarx::ThreadPool::Handle::Handle
Handle()
Definition: ThreadPool.cpp:150
armarx::ThreadPool::Handle
Definition: ThreadPool.h:52
armarx::ThreadPool
The ThreadPool class.
Definition: ThreadPool.h:45
armarx::ThreadPool::getAvailableTaskCount
int getAvailableTaskCount() const
If queing is disabled, returns the number of available threads.
Definition: ThreadPool.cpp:109
armarx::ThreadPool::Handle::isJoined
bool isJoined() const
Definition: ThreadPool.cpp:216
armarx::ThreadPool::Handle::isDetached
bool isDetached() const
Definition: ThreadPool.cpp:222
armarx::ThreadPool::Impl
Definition: ThreadPool.cpp:41
armarx::ThreadPool::Handle::join
void join()
Definition: ThreadPool.cpp:175
armarx::ThreadPool::~ThreadPool
~ThreadPool()
Destructor.
Definition: ThreadPool.cpp:66
armarx::ThreadPool::Handle::~Handle
~Handle() noexcept(false)
Definition: ThreadPool.cpp:159
armarx::ThreadPool::Handle::detach
void detach()
Definition: ThreadPool.cpp:193
armarx::ThreadPool::Handle::getFuture
const std::shared_future< void > & getFuture() const
Definition: ThreadPool.cpp:210
armarx::ThreadPool::Handle::operator=
Handle & operator=(Handle &&)=default
armarx::ThreadPool::Handle::isValid
bool isValid() const
Definition: ThreadPool.cpp:169
armarx::ThreadPool::runTask
Handle runTask(std::function< void()> task)
Adds a task to the thread pool if a thread is currently available.
Definition: ThreadPool.cpp:82
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27