Number5
Visualisierung 2 Project - Florian Schober (0828151, f.schober@live.com), Andreas Walch (0926780, walch.andreas89@gmail.com)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TaskManager.cpp
Go to the documentation of this file.
1 #include "Application.hpp"
2 
3 
5  : m_signaled(signaled)
6 { }
7 
9 {
10  {
11  lock_guard<mutex> lock(m_mutex);
12  m_signaled = true;
13  }
14 
15  m_cv.notify_all();
16 }
17 
19 {
20  lock_guard<mutex> lock(m_mutex);
21  m_signaled = false;
22 }
23 
24 
26 {
27  unique_lock<mutex> lock(m_mutex);
28 
29  while (!m_signaled)
30  m_cv.wait(lock);
31 }
32 
33 
34 
35 
37  : m_signal(0)
38 { }
39 
41 {
42  {
43  lock_guard<mutex> lock(m_mutex);
44  m_signal++;
45 
46  m_cv.notify_one();
47  }
48 }
49 
51 {
52  {
53  lock_guard<mutex> lock(m_mutex);
54  m_signal += n;
55 
56  m_cv.notify_all();
57  }
58 }
59 
60 
61 void auto_reset_event::wait(uint nTimes)
62 {
63  unique_lock<mutex> lock(m_mutex);
64 
65  m_cv.wait(lock, [this, nTimes]() { return m_signal >= nTimes; });
66  m_signal -= nTimes;
67 }
68 
69 
70 
71 
73 {
74  auto const threadCount = app->getConfig().threadpoolSize();
75 
76  cout << "Threadpool-Size: " << threadCount << endl;
77 
78  m_threadKeepAlive.reserve(threadCount);
79  m_threads.reserve(threadCount);
80 
81  for (size_t i = 0; i < threadCount; i++)
82  {
83  m_threadKeepAlive.push_back(true);
84  m_threads.push_back(thread(&TaskManager::worker, this, &(m_threadKeepAlive[i])));
85  }
86 }
88 {
89  for (size_t i = 0; i < m_threads.size(); i++)
90  m_threadKeepAlive[i] = false;
91 
92  for (size_t i = 0; i < m_threads.size(); i++)
94 
95  for (auto& thread : m_threads)
96  thread.join();
97 }
98 
99 void TaskManager::worker(BOOL* keepAlive)
100 {
101  function<void()> task;
102 
103  while (*keepAlive)
104  {
106 
107  if (m_taskqueue.try_pop(task))
108  task();
109  }
110 }
TaskManager(Application *app)
Definition: TaskManager.cpp:72
bool m_signaled
Wheter or not the event is currently signalled.
Definition: TaskManager.hpp:14
Configuration & getConfig()
Definition: Application.hpp:36
void reset()
Sets the event to unsignaled-mode.
Definition: TaskManager.cpp:18
uint m_signal
Wheter or not the event is currently signalled.
Definition: TaskManager.hpp:40
uint threadpoolSize() const
void signal()
Sets the event to signaled-mode.
Definition: TaskManager.cpp:8
condition_variable m_cv
A condition variable used for synchronization.
Definition: TaskManager.hpp:39
vector< thread > m_threads
The thread object for all worker-threads.
Definition: TaskManager.hpp:65
manual_reset_event(bool signaled=false)
Definition: TaskManager.cpp:4
auto_reset_event m_threadHasTodo
An event that tells workers wheter or not there is a task.
Definition: TaskManager.hpp:64
condition_variable m_cv
A condition variable used for synchronization.
Definition: TaskManager.hpp:13
void signal()
Sets the event to signaled-mode.
Definition: TaskManager.cpp:40
vector< BOOL > m_threadKeepAlive
A a bool for each working-thread that tells it wheter or not to stay alive.
Definition: TaskManager.hpp:63
void worker(BOOL *keepAlive)
Definition: TaskManager.cpp:99
void signalN(uint n)
Sets the event to signaled-mode (N-times, i.e. to wake up N threads)
Definition: TaskManager.cpp:50
mutex m_mutex
A mutex used for synchronization.
Definition: TaskManager.hpp:38
void wait(uint nTimes=1)
Wait (block) until the event is set to signaled-mode.
Definition: TaskManager.cpp:61
concurrency::concurrent_queue< function< void()> > m_taskqueue
A queue of tasks that needs to be executed.
Definition: TaskManager.hpp:62
mutex m_mutex
A mutex used for synchronization.
Definition: TaskManager.hpp:12
void wait()
Wait (block) until the event is set to signaled-mode.
Definition: TaskManager.cpp:25