GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
ThreadPool.h
Go to the documentation of this file.
1 
11 /*
12 GRT MIT License
13 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
14 
15 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
16 and associated documentation files (the "Software"), to deal in the Software without restriction,
17 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
19 subject to the following conditions:
20 
21 The above copyright notice and this permission notice shall be included in all copies or substantial
22 portions of the Software.
23 
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
25 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 */
30 
31 #ifndef GRT_THREAD_POOL_HEADER
32 #define GRT_THREAD_POOL_HEADER
33 
34 //Include the common C++ headers
35 #include <vector>
36 #include <queue>
37 #include <stdexcept>
38 
39 #ifdef GRT_CXX11_ENABLED
40 #include <memory>
41 #include <thread>
42 #include <mutex>
43 #include <condition_variable>
44 #include <future>
45 #include <functional>
46 #endif //GRT_CXX11_ENABLED
47 
48 #include "GRTTypedefs.h"
49 
50 GRT_BEGIN_NAMESPACE
51 
52 class GRT_API ThreadPool {
53 public:
54 
58  ThreadPool();
59 
65  ThreadPool(const unsigned int numThreads);
66 
70  ~ThreadPool();
71 
72 #ifdef GRT_CXX11_ENABLED
73 
83  template<class F, class... Args>
84  auto enqueue(F&& f, Args&&... args) -> std::future< typename std::result_of<F(Args...) >::type>;
85 #endif //GRT_CXX11_ENABLED
86 
93  static unsigned int getThreadPoolSize();
94 
101  static bool setThreadPoolSize( const unsigned int threadPoolSize );
102 
103 protected:
104 #ifdef GRT_CXX11_ENABLED
105  void launchThreads(const unsigned int threads);
106 
107  std::vector< std::thread > workers;
108  std::queue< std::function<void()> > tasks;
109 
110  // synchronization
111  std::mutex queue_mutex;
112  std::condition_variable condition;
113  std::atomic< bool > stop;
114 
115  static std::atomic< unsigned int > threadPoolSize;
116 #endif //GRT_CXX11_ENABLED
117 };
118 
119 #ifdef GRT_CXX11_ENABLED
120 // This function adds a new work item (func) to the thread pool
121 template<class F, class... Args> auto ThreadPool::enqueue(F&& func, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type>
122 {
123  //Get a shortcut to the return type of the input function
124  using return_type = typename std::result_of< F(Args...) >::type;
125 
126  auto task = std::make_shared< std::packaged_task< return_type() > >( std::bind(std::forward<F>( func ), std::forward<Args>(args)...) );
127 
128  std::future< return_type > res = task->get_future();
129  {
130  std::unique_lock< std::mutex > lock( queue_mutex );
131 
132  // don't allow enqueueing after stopping the pool
133  if( stop )
134  throw std::runtime_error("enqueue on stopped ThreadPool");
135 
136  tasks.emplace( [task](){ (*task)(); } );
137  }
138  condition.notify_one();
139  return res;
140 }
141 #endif //GRT_CXX11_ENABLED
142 
143 GRT_END_NAMESPACE
144 
145 #endif //GRT_THREAD_POOL_HEADER