Skip to content

Commit 622f24a

Browse files
committed
‘提交’
1 parent aff1560 commit 622f24a

File tree

9 files changed

+1297
-1
lines changed

9 files changed

+1297
-1
lines changed

README.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,100 @@
11
# Priority_Threads_Pool
2-
支持优先级调度SPF和先来先服务FCFS的线程池,基于开源项目ctpl拓展
2+
3+
支持优先级调度SPF和先来先服务FCFS的线程池,基于开源项目ctpl (https://github.com/vit-vit/CTPL)拓展
4+
5+
#文件说明
6+
PriorityThreadsPool文件夹:vs2019工程项目
7+
Priority_Threads_Pool.h 线程库文件
8+
test.cpp 测试用例
9+
10+
#实现新功能
11+
12+
1.自定义优先级的任务队列,在push时指定
13+
14+
```
15+
#include "priorityThreadPool.h"
16+
17+
PriorityThreadPool pl(2);
18+
pl.push(1,test1,params...);//第一个函数是优先级,后面是执行函数和参数表
19+
```
20+
21+
2.删除了传线程id的参数,无需二次包装
22+
```
23+
//需要执行的任务
24+
void test() {
25+
Sleep(2000);
26+
cout << "helloworld" << endl;
27+
}
28+
//ctpl库中函数必须接收线程id参数,因此需要包装一层函数
29+
void package(int id) {
30+
test();
31+
}
32+
33+
int main(){
34+
//ctpl中
35+
ctpl::thread_pool p(2);//2个线程的线程池
36+
p.push(package);
37+
38+
//ptpl中
39+
FCFS_ThreadPool pl(2);
40+
pl.push(test);
41+
42+
}
43+
```
44+
3.任务队列独立封装,提高了可拓展性
45+
```
46+
//改写TaskQueue类实现自定义任务调度,Priority_Threads_Pool在此基础上实现
47+
class TaskQueue{
48+
...
49+
}
50+
```
51+
52+
4.脱离了对boost库的依赖,基于STL queue库实现了线程安全的任务队列
53+
54+
5.支持完美转发(ctpl已实现,Priority_Threads_Pool保留了这一特点)
55+
56+
6.根据自己理解,修改了变量命名,并且在基类上提供了详细的中文注释
57+
58+
#更新计划
59+
· 将实现自适应大小的线程池,线程长时间阻塞时自动释放,自动均衡负载。
60+
61+
#快速开始
62+
```
63+
#include <iostream>
64+
#include <functional>
65+
#include <Windows.h>
66+
#include "priorityThreadPool.h"
67+
using namespace std;
68+
using namespace ptpl;
69+
void test1() {
70+
Sleep(2000);
71+
cout << "helloworld" << endl;
72+
}
73+
void test2(int a) {
74+
Sleep(2000);
75+
cout << "prioritytask" <<a<< endl;
76+
}
77+
int main() {
78+
FCFS_ThreadPool pl(2);
79+
pl.push(test1);
80+
pl.push(test1);
81+
pl.push(test1);
82+
pl.push(test1);
83+
pl.push(test1);
84+
pl.push(test1);
85+
86+
PriorityThreadPool ptpl(2);
87+
ptpl.push(1,test1);
88+
ptpl.push(1,test1);
89+
ptpl.push(1,test1);
90+
ptpl.push(1,test1);
91+
ptpl.push(2,test2,3);
92+
ptpl.push(2,test2,4);
93+
return 0;
94+
}
95+
```
96+
97+
#原理描述
98+
1.任务队列:将传入的函数使用`std::package_task`转为可执行对象保存在任务队列中,使用优先队列来实现优先级调度。
99+
100+
2.线程执行:每一个线程完成**从任务队列中获取任务-执行任务**的循环,当任务队列为空时通过条件变量阻塞,直到提交新的任务后将线程唤醒。

0 commit comments

Comments
 (0)