-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.h
More file actions
35 lines (29 loc) · 743 Bytes
/
ThreadPool.h
File metadata and controls
35 lines (29 loc) · 743 Bytes
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
#pragma once
#include <vector>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
class ThreadPool
{
public:
ThreadPool(int n);
~ThreadPool();
template <typename Func, typename... Args>
void addTask(Func &&func, Args &&...args)
{
std::packaged_task<void()> task{std::bind(std::forward<Func>(func), std::forward<Args>(args)...)};
{
std::unique_lock<std::mutex> lock(m_mtx);
m_tasks.emplace(std::move(task));
}
m_cv.notify_one();
}
private:
std::queue<std::packaged_task<void()>> m_tasks;
std::vector<std::thread> m_workers;
std::mutex m_mtx;
std::condition_variable m_cv;
bool is_stop;
};