-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_pool.h
More file actions
33 lines (29 loc) · 757 Bytes
/
Copy paththread_pool.h
File metadata and controls
33 lines (29 loc) · 757 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
#ifndef __THREAD_POOL__
#define __THREAD_POOL__
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <queue>
#include <functional>
#include <atomic>
class ThreadPool {
public:
using Task = std::function<void()>;
ThreadPool(int thread_num);
~ThreadPool();
void quit();
void add_task(const Task& task);
int get_thread_num() const;
int get_task_num();
private:
std::vector<std::thread> _pool;
std::queue<Task> _tasks;
std::mutex _mutex;
std::condition_variable _cond_var;
std::atomic<bool> _quit;
void run_task(int thread_index);
ThreadPool(const ThreadPool& thread_pool) = delete;
ThreadPool& operator=(const ThreadPool& thread_pool) = delete;
};
#endif