资源简介
用c++ 实现的线程池工程,可直接使用,其中线程数目可根据空闲情况自增减,并且实现线程跟任务无关联。

代码片段和文件信息
#include
#include
#include “threadpoolmanager.h“
#include
#include
#include
#include
using namespace std;
int seq = 0;
int billNum =0;
int inter = 1;
pthread_mutex_t m_mutex;
void myFunc(void*arg)
{
pthread_mutex_lock(&m_mutex);
seq++;
if(seq%inter == 0 )
{
cout << “fun 1=“ << seq << endl;
}
if(seq>=1000000000)
{
cout << “billion“ << endl;
seq = 0;
billNum++;
}
pthread_mutex_unlock(&m_mutex);
//sleep();
}
int main(int argc char** argv)
{
if(argc != 6)
{
cout << “必须有5个参数 任务执行次数 任务池大小 线程池大小 预创建线程数 输出间隔“ << endl;
cout << “eg: ./test 999999 10000 100 10 20“ << endl;
cout << “上例代表创建一个间隔20个任务输出,任务池大小为10000,线程池大小为100,预创建10个线程,执行任务次数为:999999“ << endl;
return 0;
}
double loopSize = atof(argv[1]);
int taskSize = atoi(argv[2]);
int threadPoolSize = atoi(argv[3]);
int preSize = atoi(argv[4]);
inter = atoi(argv[5]);
pthread_mutex_init(&m_mutexNULL);
ThreadPoolManager manager;
if(0>manager.Init(taskSize threadPoolSize preSize))
{
cout << “初始化失败“ << endl;
return 0;
}
cout << “*******************初始化完成*********************“ << endl;
struct timeval time_beg time_end;
memset(&time_beg 0 sizeof(struct timeval));
memset(&time_end 0 sizeof(struct timeval));
gettimeofday(&time_beg NULL);
double i=0;
for(; i {
while(0>manager.Run(myFuncNULL))
{
usleep(100);
}
}
gettimeofday(&time_end NULL);
long total = (time_end.tv_sec - time_beg.tv_sec)*1000000 + (time_end.tv_usec - time_beg.tv_usec);
cout << “total time =“ << total << endl;
cout << “total num =“ << i << “ billion num=“ << billNum<< endl;
cout << __FILE__ << “将关闭所有线程“ << endl;
//pthread_mutex_destroy(&m_mutex);
return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
....... 440 2018-12-20 14:48 threadpool\commondef.h
....... 2143 2018-12-20 14:51 threadpool\main.cpp
....... 468 2018-12-20 16:26 threadpool\Makefile
....... 4670 2018-12-20 14:48 threadpool\taskpool.cpp
....... 2569 2018-12-20 14:49 threadpool\taskpool.h
....... 7999 2018-12-20 14:53 threadpool\threadpool.cpp
....... 3085 2018-12-20 14:50 threadpool\threadpool.h
....... 4111 2018-12-20 14:50 threadpool\threadpoolmanager.cpp
....... 3528 2018-12-20 14:51 threadpool\threadpoolmanager.h
目录 0 2018-12-20 16:59 threadpool
----------- --------- ---------- ----- ----
29013 10
相关资源
- 国际象棋的qt源代码
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- VC++MFC小游戏实例教程(实例)+MFC类库
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- FTP客户端源码(c++)
- c++ 画图(14Qt-XPS)
- c++多边形交并差运算
评论
共有 条评论