-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadPool.h
57 lines (41 loc) · 1.11 KB
/
ThreadPool.h
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* ThreadPool.hpp
*
* Created on: May 24, 2020
* Author: voukatas
*/
#ifndef THREADPOOL_HPP_
#define THREADPOOL_HPP_
#include <functional>
#include <vector>
#include <thread>
#include <queue>
#include <condition_variable>
#include <iostream>
namespace ThreadPoolSpace
{
class ThreadPool{
using Job = std::function<void()>;
private:
//keep track of threads for cleanup
std::vector<std::thread> workers;
std::queue<Job> jobQ;
std::mutex jobQMutex;
std::condition_variable jobQCv;
bool stopExecution = false;
public:
explicit ThreadPool(std::size_t numOfWorkers);
virtual ~ThreadPool();
ThreadPool() = delete;
//delete copy/move constructors and assgiments
ThreadPool(const ThreadPool&) = delete;
ThreadPool& operator=(const ThreadPool&) = delete;
ThreadPool( ThreadPool&&) = delete;
ThreadPool& operator=(ThreadPool&&) = delete;
void addJob(Job job);
private:
void start(std::size_t numOfWorkers);
void shutdown();
};
}
#endif /* THREADPOOL_HPP_ */