-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.h
More file actions
41 lines (30 loc) · 878 Bytes
/
Copy paththreadpool.h
File metadata and controls
41 lines (30 loc) · 878 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
36
37
38
39
40
41
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <list>
#include <thread>
#include <functional>
#include <memory>
#include <atomic>
#include "syncqueue.h"
const int TaskMaxSize = 10;
class ThreadPool
{
public:
using Task = std::function<void()>;
//参数默认值放在声明中,初始化列表放在实现中
ThreadPool(int numThreads = std::thread::hardware_concurrency());
~ThreadPool(void);
void CreateThreadGroup(int numThreads);
void Function();
void AddTask(Task&&task);
void AddTask(const Task &task);
void DestroyThreadGroup();
private:
//容器对象
std::list<std::shared_ptr<std::thread>> m_threadgroup; //线程组
//模板类对象
SyncQueue<Task> m_syncqueue; //同步队列
std::atomic_bool m_threadpool_alive; //线程池是否活着
std::once_flag m_flag; //只调用一次的标志
};
#endif