-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTest.cpp
More file actions
39 lines (32 loc) · 845 Bytes
/
Test.cpp
File metadata and controls
39 lines (32 loc) · 845 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
#include "PthreadPool.h"
int tasks = 0, done = 0;
pthread_mutex_t lock;
void dummy_task(void *arg)
{
usleep(100000);
pthread_mutex_lock(&lock);
/* 记录成功完成的任务数 */
cout << "Tid:" << pthread_self() << " " << done++ << endl;
pthread_mutex_unlock(&lock);
}
int main(int argc, char **argv)
{
/* 初始化互斥锁 */
pthread_mutex_init(&lock, NULL);
PthreadPool pool;
pool.Init(4);
/* 只要任务队列还没满,就一直添加 */
while (pool.AddTask(&dummy_task, NULL) == 0)
{
pthread_mutex_lock(&lock);
tasks++;
pthread_mutex_unlock(&lock);
// cout << tasks << endl;
if (tasks == 100)
break;
}
cout << "add tasks:" << done << endl;
pool.Destory();
cout << "Did tasks:" << done << endl;
return 0;
}