#include
#include "thread.h"
#include "threadpool.h"
/**
* 各任务的处理函数
*
*/
int function1(void* data);
int function2(void* data);
int function3(void* data);
int function4(void* data);
int function5(void* data);
int function6(void* data);
int function7(void* data);
int function8(void* data);
int main(int argc, char** argv)
{
ThreadPool threadPool;
if(threadPool.init(4))
{
std::cout<<"[ERROR] threadPool.init failed"
<< std::endl;
return -1;
}
// 循环次数
int n = 1000000000;
// Run task 1
TASK task1;
task1.task = &function1;
task1.data = (void*)(&n);
L1: Thread* pThread = threadPool.getIdelThread();
if(pThread != NULL)
{
pThread->setTask(&task1);
pThread->start();
}
else
{
//std::cout<<"[INFO] there is no idel thread in the pool"
// << std::endl;
// 再次尝试获得空闲的Thread,这里是不可能的,这时肯定有四个空闲的Thread
goto L1;
}
// Run task 2
TASK task2;
task2.task = &function2;
task2.data = (void*)(&n);
L2: pThread = threadPool.getIdelThread();
if(pThread != NULL)
{
pThread->setTask(&task2);
pThread->start();
}
else
{
//std::cout<<"[INFO] there is no idel thread in the pool"
// << std::endl;
// 再次尝试获得空闲的Thread,这里是不可能的,这时至少有三个空闲的Thread
goto L2;
}
// Run task 3
TASK task3;
task3.task = &function3;
task3.data = (void*)(&n);
L3: pThread = threadPool.getIdelThread();
if(pThread != NULL)
{
pThread->setTask(&task3);
pThread->start();
}
else
{
//std::cout<<"[INFO] there is no idel thread in the pool"
// << std::endl;
// 再次尝试获得空闲的Thread,这里是不可能的,这时至少有两个空闲的Thread
goto L3;
}
// Run task 4
TASK task4;
task4.task = &function4;
task4.data = (void*)(&n);
L4: pThread = threadPool.getIdelThread();
if(pThread != NULL)
{
pThread->setTask(&task4);
pThread->start();
}
else
{
//std::cout<<"[INFO] there is no idel thread in the pool"
// << std::endl;
// 再次尝试获得空闲的Thread,这里是不可能的,这时至少有一个空闲的Thread
goto L4;
}
// Run task 5
TASK task5;
task5.task = &function5;
task5.data = (void*)(&n);
L5: pThread = threadPool.getIdelThread();
if(pThread != NULL)
{
pThread->setTask(&task5);
pThread->start();
}
else
{
//std::cout<<"[INFO] there is no idel thread in the pool"
// << std::endl;
// 再次尝试获得空闲的Thread,这里是可能的
goto L5;
}
// Run task 6
TASK task6;
task6.task = &function6;
task6.data = (void*)(&n);
L6: pThread = threadPool.getIdelThread();
if(pThread != NULL)
{
pThread->setTask(&task6);
pThread->start();
}
else
{
//std::cout<<"[INFO] there is no idel thread in the pool"
// << std::endl;
// 再次尝试获得空闲的Thread,这里是可能的
goto L6;
}
// Run task 7
TASK task7;
task7.task = &function7;
task7.data = (void*)(&n);
L7: pThread = threadPool.getIdelThread();
if(pThread != NULL)
{
pThread->setTask(&task7);
pThread->start();
}
else
{
//std::cout<<"[INFO] there is no idel thread in the pool"
// << std::endl;
// 再次尝试获得空闲的Thread,这里是可能的
goto L7;
}
// Run task 8
TASK task8;
task8.task = &function8;
task8.data = (void*)(&n);
L8: pThread = threadPool.getIdelThread();
if(pThread != NULL)
{
pThread->setTask(&task8);
pThread->start();
}
else
{
//std::cout<<"[INFO] there is no idel thread in the pool"
// << std::endl;
// 再次尝试获得空闲的Thread,这里是可能的
goto L8;
}
// 等待所有任务完成
while(1)
{
}
return 0;
}
/**
* 任务1的处理函数,只是空循环浪费CPU时间
*
*/
int function1(void* data)
{
int n = *((int*)(data));
std::cout<<"[INFO] task1 running"
<
for(int i = 0; i < n; i++)
{
}
std::cout<<"[INFO] task1 end"
<
return 0;
}
/**
* 任务2的处理函数,只是空循环浪费CPU时间
*
*/
int function2(void* data)
{
int n = *((int*)(data));
std::cout<<"[INFO] task2 running"
<
for(int i = 0; i < n; i++)
{
}
std::cout<<"[INFO] task2 end"
<
return 0;
}
/**
* 任务3的处理函数,只是空循环浪费CPU时间
*
*/
int function3(void* data)
{
int n = *((int*)(data));
std::cout<<"[INFO] task3 running"
<
for(int i = 0; i < n; i++)
{
}
std::cout<<"[INFO] task3 end"
<
return 0;
}
/**
* 任务4的处理函数,只是空循环浪费CPU时间
*
*/
int function4(void* data)
{
int n = *((int*)(data));
std::cout<<"[INFO] task4 running"
<
for(int i = 0; i < n; i++)
{
}
std::cout<<"[INFO] task4 end"
<
return 0;
}
/**
* 任务5的处理函数,只是空循环浪费CPU时间
*
*/
int function5(void* data)
{
int n = *((int*)(data));
std::cout<<"[INFO] task5 running"
<
for(int i = 0; i < n; i++)
{
}
std::cout<<"[INFO] task5 end"
<
return 0;
}
/**
* 任务6的处理函数,只是空循环浪费CPU时间
*
*/
int function6(void* data)
{
int n = *((int*)(data));
std::cout<<"[INFO] task6 running"
<
for(int i = 0; i < n; i++)
{
}
std::cout<<"[INFO] task6 end"
<
return 0;
}
/**
* 任务7的处理函数,只是空循环浪费CPU时间
*
*/
int function7(void* data)
{
int n = *((int*)(data));
std::cout<<"[INFO] task7 running"
<
for(int i = 0; i < n; i++)
{
}
std::cout<<"[INFO] task7 end"
<
return 0;
}
/**
* 任务8的处理函数,只是空循环浪费CPU时间
*
*/
int function8(void* data)
{
int n = *((int*)(data));
std::cout<<"[INFO] task8 running"
<
for(int i = 0; i < n; i++)
{
}
std::cout<<"[INFO] task8 end"
7. 呵呵,就是这么简单,估计肯定有BUG,希望你能在发现了BUG邮件我,我的邮件iacrqq@gmail.com,欢迎大家指出不足之处,也欢迎大家和我交流。
这个程序代码文件和Makefile,在我上传的文件,有意请下载。
阅读(1214) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~
chinaunix网友2010-08-21 16:25:12
暴强!
可惜我不懂c++,不然肯定download下来研究了。