-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadPool.hpp
33 lines (30 loc) · 885 Bytes
/
ThreadPool.hpp
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
#pragma once
#include <iostream>
#include <thread>
#include <list>
#include <algorithm>
#include <chrono>
#include <vector>
#include <mutex>
#include <functional>
#include <condition_variable>
#include <queue>
namespace Can
{
class ThreadPool
{
private:
void ThreadLoop();
std::mutex queue_mutex; // Prevents data races to the job queue
std::condition_variable mutex_condition; // Allows threads to wait on new jobs or termination
std::vector<std::thread> threads;
std::queue<std::function<void()>> jobs;
uint16_t pool_size; // Number of threads to be
bool should_terminate = false; // Tells threads to stop looking for jobs
public:
ThreadPool(uint16_t pool_size);
void Start();
void QueueJob(const std::function<void()> &job);
void Stop();
};
}