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.cpp
1 #include "ThreadPool.h"
2 #include <iostream>
3 
4 using namespace GRT;
5 
6 #ifdef GRT_CXX11_ENABLED
7 //Initalize the static thread pool size to the systems suggested thread limit
8 std::atomic< unsigned int > ThreadPool::threadPoolSize( std::thread::hardware_concurrency() );
9 #endif
10 
12 {
13 #ifdef GRT_CXX11_ENABLED
14  stop = false;
15  launchThreads( threadPoolSize );
16 #endif
17 }
18 
19 ThreadPool::ThreadPool(const unsigned int poolSize)
20 {
21 #ifdef GRT_CXX11_ENABLED
22  stop = false;
23  launchThreads( poolSize );
24 #endif
25 }
26 
27 // the destructor joins all threads
29 {
30 std::cout << "~ThreadPool()\n";
31 #ifdef GRT_CXX11_ENABLED
32  stop = true;
33  condition.notify_all();
34  for(std::thread &worker: workers)
35  worker.join();
36 #endif
37 }
38 
39 #ifdef GRT_CXX11_ENABLED
40 void ThreadPool::launchThreads( const unsigned int poolSize ){
41 
42  //Start the worker thread, each thread will wait for incoming tasks and pop them off the queue when ready
43  for(unsigned int i = 0; i<poolSize; ++i)
44  workers.emplace_back(
45  [this]
46  {
47  while( true )
48  {
49  std::function< void() > task;
50 
51  //Lock the queue and wait for the condition to change
52  {
53  std::unique_lock<std::mutex> lock(this->queue_mutex);
54  this->condition.wait(lock,
55  [this]{ return this->stop || !this->tasks.empty(); });
56  if(this->stop && this->tasks.empty()){
57  std::cout << "stopping worker!" << std::endl;
58  return;
59  }
60  task = std::move(this->tasks.front());
61  this->tasks.pop();
62  }
63 
64 
65  //Otherwise grab the task from the front of the buffer
66  if( task ){
67  //Run the task
68  task();
69  }
70 
71  }
72  }
73  );
74 }
75 #endif
76 
77 unsigned int ThreadPool::getThreadPoolSize(){
78 #ifdef GRT_CXX11_ENABLED
79  return threadPoolSize;
80 #else
81  return 0;
82 #endif
83 }
84 
85 bool ThreadPool::setThreadPoolSize( const unsigned int threadPoolSize_ ){
86 #ifdef GRT_CXX11_ENABLED
87  threadPoolSize = threadPoolSize_;
88  return true;
89 #else
90  return false;
91 #endif
92 }
93 
Definition: DebugLog.cpp:23
The ThreadPool class implements a flexible inteface for performing a large number of batch tasks...
static bool setThreadPoolSize(const unsigned int threadPoolSize)
Definition: ThreadPool.cpp:85