使用boost中的线程池
2015-05-11 11:15
155 查看
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
using namespace boost;
using namespace std;
void runChild(const int n)
{
cout << "我是第" << n << "个子线程" << endl;
sleep(1);
cout << "进程" << n << "退出" << endl;
}
int main(int argc, char** argv)
{
int num;
thread_group threads;
if (argc < 2)
{
cout << "请提供一个要生成线程数的参数" << endl;
exit(-1);
}
num = atoi(argv[1]);
cout << "我是主程序,我准备产生" << num << "个子线程" << endl;
for(int i = 0; i < num; i++)
{
threads.create_thread(bind(&runChild, i));
}
cout << "我是主程序,我在等子线程运行结束" << endl;
threads.join_all();
return 0;
}
编译&测试(我在我的ubuntu下测试的)
> g++ threadgroup.cc -lboost_thread
> ./a.out 3
如果在freebsd4下编译的话,如果使用pthread作为线程实现的话,需要明确指出pthread使用线程库,而且默认的template深度好像不能满足boost的需求..需要在编译时加上:
-ftemplate-depth-20 -boost
#include <boost/bind.hpp>
#include <iostream>
using namespace boost;
using namespace std;
void runChild(const int n)
{
cout << "我是第" << n << "个子线程" << endl;
sleep(1);
cout << "进程" << n << "退出" << endl;
}
int main(int argc, char** argv)
{
int num;
thread_group threads;
if (argc < 2)
{
cout << "请提供一个要生成线程数的参数" << endl;
exit(-1);
}
num = atoi(argv[1]);
cout << "我是主程序,我准备产生" << num << "个子线程" << endl;
for(int i = 0; i < num; i++)
{
threads.create_thread(bind(&runChild, i));
}
cout << "我是主程序,我在等子线程运行结束" << endl;
threads.join_all();
return 0;
}
编译&测试(我在我的ubuntu下测试的)
> g++ threadgroup.cc -lboost_thread
> ./a.out 3
如果在freebsd4下编译的话,如果使用pthread作为线程实现的话,需要明确指出pthread使用线程库,而且默认的template深度好像不能满足boost的需求..需要在编译时加上:
-ftemplate-depth-20 -boost
相关文章推荐
- boost_group线程池的使用
- boost asio--作为线程池使用
- boost之线程池使用实例
- 使用boost的多线程库写的线程池测试用例
- 使用boost中的线程池
- boost 中使用 线程池threadpool
- boost中使用线程池threadpool
- boost 线程池的使用
- Boost::thread库的使用
- boost 使用
- Socket网络编程学习笔记(6):使用线程池提高性能
- Java并发编程:线程池的使用
- 在libevent中使用线程池
- Boost::thread库的使用
- QT线程池实验研究与分析(QThread与和QThreadPool + QRunnable使用上的区别)
- Java Executor并发框架(十四)Executor框架线程池使用原始方式实现生产者消费者模式
- C++11 下使用 Boost.Serialization 库实现智能指针的序列化
- Java四种线程池的使用
- JDK线程池的使用
- SpringMVC整合TaskExecutor线程池的配置/使用