-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThread.cpp
40 lines (34 loc) · 826 Bytes
/
Thread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// Created by wojtowic on 30.07.17.
//
#include "Thread.h"
Thread::Thread(std::queue<std::function<void()>>& queue, std::mutex& queueMutex, std::condition_variable& queueCondVar)
:
m_queue(queue),
m_queueMutex(queueMutex),
m_queueCondVar(queueCondVar)
{
m_worker = std::thread(&Thread::work, this);
}
Thread::~Thread() {
deleting = true;
m_worker.join();
}
void Thread::work() {
while(true) {
std::function<void()> job;
{
std::unique_lock<std::mutex> lock(m_queueMutex);
m_queueCondVar.wait(lock, [&]{ return !m_queue.empty() || deleting; });
if(deleting) {
break;
}
job = m_queue.front();
m_queue.pop();
}
job();
}
}
void Thread::finish() {
deleting = true;
}