GestureRecognitionToolkit  Version: 0.1.0
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 namespace GRT{
49 
50 class ThreadPool {
51 public:
52 
56  ThreadPool();
57 
63  ThreadPool(const unsigned int numThreads);
64 
68  ~ThreadPool();
69 
70 #ifdef GRT_CXX11_ENABLED
71 
81  template<class F, class... Args>
82  auto enqueue(F&& f, Args&&... args) -> std::future< typename std::result_of<F(Args...) >::type>;
83 #endif //GRT_CXX11_ENABLED
84 
91  static unsigned int getThreadPoolSize();
92 
99  static bool setThreadPoolSize( const unsigned int threadPoolSize );
100 
101 protected:
102 #ifdef GRT_CXX11_ENABLED
103  void launchThreads(const unsigned int threads);
104 
105  std::vector< std::thread > workers;
106  std::queue< std::function<void()> > tasks;
107 
108  // synchronization
109  std::mutex queue_mutex;
110  std::condition_variable condition;
111  std::atomic< bool > stop;
112 
113  static std::atomic< unsigned int > threadPoolSize;
114 #endif //GRT_CXX11_ENABLED
115 };
116 
117 #ifdef GRT_CXX11_ENABLED
118 // This function adds a new work item (func) to the thread pool
119 template<class F, class... Args> auto ThreadPool::enqueue(F&& func, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type>
120 {
121  //Get a shortcut to the return type of the input function
122  using return_type = typename std::result_of< F(Args...) >::type;
123 
124  auto task = std::make_shared< std::packaged_task< return_type() > >( std::bind(std::forward<F>( func ), std::forward<Args>(args)...) );
125 
126  std::future< return_type > res = task->get_future();
127  {
128  std::unique_lock< std::mutex > lock( queue_mutex );
129 
130  // don't allow enqueueing after stopping the pool
131  if( stop )
132  throw std::runtime_error("enqueue on stopped ThreadPool");
133 
134  tasks.emplace( [task](){ (*task)(); } );
135  }
136  condition.notify_one();
137  return res;
138 }
139 #endif //GRT_CXX11_ENABLED
140 
141 }
142 
143 #endif //GRT_THREAD_POOL_HEADER
static unsigned int getThreadPoolSize()
Definition: ThreadPool.cpp:77
Definition: DebugLog.cpp:23
static bool setThreadPoolSize(const unsigned int threadPoolSize)
Definition: ThreadPool.cpp:85