-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskQueue.h
More file actions
104 lines (91 loc) · 2.06 KB
/
Copy pathTaskQueue.h
File metadata and controls
104 lines (91 loc) · 2.06 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#pragma once
#include <queue>
#include <functional>
#include <memory>
using namespace std;
using callback = void (*)(void* arg);
//任务结构体
//template<class T>
//struct Task
//{
// //struct在C++中也相当于一个类,因此写了一个构造函数用于初始化
// Task<T>()
// {
// function = nullptr;
// arg = nullptr;
// }
// Task<T>(callback f, void* arg)
// {
// this->arg =static_cast<T*>(arg); //显式类型转换
// function = f;
// }
// callback function;//函数指针
// T* arg;
//};
//使用std::function来增加灵活性,并使用lambda表达式,避免适配器函数
template<class T>//这里的T应该是std::shared_ptr<Connection>
struct Task
{
Task():function(nullptr),arg(nullptr){}
/*Task(callback f,void* arg):arg(static_cast<T*>(arg)),function(f){}*/
//添加新的构造函数,接收std::function
//Task(std::function<void(void*)>f,void* arg):function(f),arg(static_cast<T*>(arg)){}
Task(std::function<void(void*)> f,std::shared_ptr<T> a):function(f),arg(a){}
std::function<void(void*)>function;
std::shared_ptr<T> arg;
};
template<class T>
class TaskQueue
{
public:
TaskQueue()
{
pthread_mutex_init(&m_mutex, NULL);
}
~TaskQueue()
{
pthread_mutex_destroy(&m_mutex);
}
//添加任务
void addTask(Task<T> task)
{
pthread_mutex_lock(&m_mutex);
m_taskQ.push(task);
pthread_mutex_unlock(&m_mutex);
}
void addTask(callback f, std::shared_ptr<T> arg)//为了编译操作,可以添加一个重载函数
{
pthread_mutex_lock(&m_mutex);
Task<T> task;
task.function = f; //设置回调函数
task.arg = arg; //设置参数
m_taskQ.push(task); //第一次入队
m_taskQ.push(Task<T>(f, arg)); //第二次入队
pthread_mutex_unlock(&m_mutex);
}
//添加对std::function的支持
void addTask(std::function<void(void*)>f, std::shared_ptr<T> arg) {
addTask(Task<T>(f, arg));
}
//取出一个任务d
Task<T> takeTask()
{
Task<T> t;
pthread_mutex_lock(&m_mutex);
if (!m_taskQ.empty())
{
t = m_taskQ.front();
m_taskQ.pop();
}
pthread_mutex_unlock(&m_mutex);
return t;
}
//获取当前任务的个数
inline size_t taskNumber()
{
return m_taskQ.size();
}
private:
pthread_mutex_t m_mutex;
queue<Task<T>> m_taskQ;
};