简单使用Boost线程池threadpool
2016-03-29 16:43
447 查看
场景:
1.在一些多线程的程序中,比如服务端响应请求时,可以同时响应多个客户端的请求,但是响应请求的个数(即线程)的个数过多的话就会造成系统资源损耗过多而宕机,还比在做一些下载的程序时,可同时开5个下载任务,对应的其实就是线程。但是最多线程是有上限的,而且每次创建线程和销毁线程都会大量损耗资源和时间。所以解决办法之一就是使用线程池控制线程个数,复用创建过的线程。
threadpool直接使用Boost库,不需要另外编译Boost库
只是开启线程,调度线程的数量,不对单个线程进程操作(比如暂停,恢复,停止)
编译的时候注意加上链接库:
LIBS := -lboost_thread
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <boost/threadpool.hpp>
using namespace std;
using namespace boost::threadpool;
void task_1()
{
cout << "task_1 start" << endl;
cout << "thead_id(task_1): " << boost::this_thread::get_id() << endl;
for (int i = 0; i < 10; i++)
{
cout << "1111111111111111111111111" << endl;
sleep(1);
}
}
void task_2()
{
cout << "task_2 start" << endl;
cout << "thead_id(task_2): " << boost::this_thread::get_id() << endl;
for (int i = 0; i < 30; i++)
{
cout << "222222222222222222222222" << endl;
sleep(1);
}
}
void DoGetVersionNoForUpdate(int a)
{
cout << "task_3 start" << endl;
cout << "thead_id(task_3): " << boost::this_thread::get_id() << endl;
for (int i = 0; i < 5; i++)
{
cout << a*a << endl;
sleep(1);
}
}
int main(int argc, char *argv[])
{
//设置允许开启的线程数
pool tp(10);
//加入线程调度,可以通过指针传参
tp.schedule(&task_1);
tp.schedule(&task_2);
int i =10;
tp.schedule(boost::bind(DoGetVersionNoForUpdate, i));
//tp.wait();
return (0);
}
1.在一些多线程的程序中,比如服务端响应请求时,可以同时响应多个客户端的请求,但是响应请求的个数(即线程)的个数过多的话就会造成系统资源损耗过多而宕机,还比在做一些下载的程序时,可同时开5个下载任务,对应的其实就是线程。但是最多线程是有上限的,而且每次创建线程和销毁线程都会大量损耗资源和时间。所以解决办法之一就是使用线程池控制线程个数,复用创建过的线程。
threadpool直接使用Boost库,不需要另外编译Boost库
只是开启线程,调度线程的数量,不对单个线程进程操作(比如暂停,恢复,停止)
编译的时候注意加上链接库:
LIBS := -lboost_thread
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <boost/threadpool.hpp>
using namespace std;
using namespace boost::threadpool;
void task_1()
{
cout << "task_1 start" << endl;
cout << "thead_id(task_1): " << boost::this_thread::get_id() << endl;
for (int i = 0; i < 10; i++)
{
cout << "1111111111111111111111111" << endl;
sleep(1);
}
}
void task_2()
{
cout << "task_2 start" << endl;
cout << "thead_id(task_2): " << boost::this_thread::get_id() << endl;
for (int i = 0; i < 30; i++)
{
cout << "222222222222222222222222" << endl;
sleep(1);
}
}
void DoGetVersionNoForUpdate(int a)
{
cout << "task_3 start" << endl;
cout << "thead_id(task_3): " << boost::this_thread::get_id() << endl;
for (int i = 0; i < 5; i++)
{
cout << a*a << endl;
sleep(1);
}
}
int main(int argc, char *argv[])
{
//设置允许开启的线程数
pool tp(10);
//加入线程调度,可以通过指针传参
tp.schedule(&task_1);
tp.schedule(&task_2);
int i =10;
tp.schedule(boost::bind(DoGetVersionNoForUpdate, i));
//tp.wait();
return (0);
}
相关文章推荐
- EditText焦点问题
- 动态规划回顾(1):最大连续子序列和
- 面试题的那些事(2)—斐波那契数列
- VC中CTreeCtrl控件单击事件,如何判断是单击的是某项的按钮,还是单击的文字
- Remastersys---制作当前的ubuntu系统镜像iso,自定义ubuntu系统
- JDBC驱动自身问题引发的FullGC
- HITS算法详解及代码实现
- JAVA打印二维码图片自定义高度
- iOS开发-git版本控制
- PHP 两张图片合成一张图片(加水印)
- Navicat异地自动备份MySQL方法详解(图文)
- iOS 原生态扫描二维码、条形码的功能。
- Gulp实战
- Linux内核实验作业五
- I.MX6 build.prop
- JavaEE_Servlet的页面跳转 和 参数的传递
- 2016.03.29///Java学习记录③
- linux 配置nfs
- x & (x - 1)式子的含义
- 异常:Can't create handler inside thread that has not called Looper.prepare()