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  public:
51 
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  operator bool() const
61  {
62  return isValid();
63  }
64  bool isValid() const;
65  void join();
66  void detach();
67  const std::shared_future<void>& getFuture() const;
68 
69  bool isDetached() const;
70  bool isJoined() const;
71 
72  private:
73  std::shared_ptr<std::mutex> mutex;
74  std::shared_future<void> functionFinished;
75  bool joined = false;
76  bool detached = false;
77  };
78 
79  /// @brief Constructor.
80  ThreadPool(std::size_t pool_size, bool queueTasks = false);
81 
82  /// @brief Destructor.
83  ~ThreadPool();
84 
85  /// @brief Adds a task to the thread pool if a thread is currently available.
86  /// @return returns true if a thread is available, otherwise returns false.
87  Handle runTask(std::function<void()> task);
88 
89  /**
90  * @brief If queing is disabled, returns the number of available threads. Otherwise returns -1;
91  */
92  int getAvailableTaskCount() const;
93 
94  private:
95  /// @brief Wrap a task so that the available count can be increased once
96  /// the user provided task has completed.
97  void wrap_task(std::function< void() > task);
98  };
99 
100 
101 }
armarx::ThreadPool::ThreadPool
ThreadPool(std::size_t pool_size, bool queueTasks=false)
Constructor.
Definition: ThreadPool.cpp:58
armarx::ThreadPool::Handle::Handle
Handle()
Definition: ThreadPool.cpp:147
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:106
armarx::ThreadPool::Handle::isJoined
bool isJoined() const
Definition: ThreadPool.cpp:211
armarx::ThreadPool::Handle::isDetached
bool isDetached() const
Definition: ThreadPool.cpp:216
armarx::ThreadPool::Impl
Definition: ThreadPool.cpp:42
armarx::ThreadPool::Handle::join
void join()
Definition: ThreadPool.cpp:173
armarx::ThreadPool::~ThreadPool
~ThreadPool()
Destructor.
Definition: ThreadPool.cpp:68
armarx::ThreadPool::Handle::~Handle
~Handle() noexcept(false)
Definition: ThreadPool.cpp:160
armarx::ThreadPool::Handle::detach
void detach()
Definition: ThreadPool.cpp:190
armarx::ThreadPool::Handle::getFuture
const std::shared_future< void > & getFuture() const
Definition: ThreadPool.cpp:206
armarx::ThreadPool::Handle::operator=
Handle & operator=(Handle &&)=default
armarx::ThreadPool::Handle::isValid
bool isValid() const
Definition: ThreadPool.cpp:168
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:81
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28