-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
50 lines (41 loc) · 1.34 KB
/
Copy pathmain.cpp
File metadata and controls
50 lines (41 loc) · 1.34 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
#include "Task.h"
#include "threadPool.h"
#include "threadPool.cpp" //由于模板类的声明和实现是分开的,所以需要包含源文件,否则就会报错“未定义的引用”
#include "TaskQueue.h"
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <iostream>
using namespace std;
//工作函数
void taskFunction(void* arg)
{
//需要对传入的地址进行类型转换,然后再解引用,得到该地址保存的数据
int num=*(int*)arg;
// printf("thread %ld is working, number = %d \n",pthread_self(),num);
cout<<"thread "<<pthread_self()<<" is working, number= "<<num<<"\n";
sleep(1);
}
int main()
{
cout<<"threadpool 向你问好! "<<"\n";
int minThread=3;
int maxThread=10;
int tasks=100;
//创建线程池
ThreadPool<int> pool(minThread,maxThread); //当线程执行完毕会自动析构,所以这里就不再需要主动调用析构函数了
//添加100个任务
//使用堆 内存
//int * num=(int *)malloc(sizeof(int));
for(int i=0;i<tasks;i++)
{
//使用堆 内存
int * num=new int(i+100); //这里的空间在work中释放了,不存在内存泄露
pool.addTask(Task<int>(taskFunction,num));
}
//保证工作线程已经处理完毕
sleep(20);
return 0;
}