boost::thread
2016-04-24 17:06
267 查看
使用无参函数创建线程
使用对象实例创建线程
通过成员函数创建线程
#include "stdafx.h" #include <iostream> #include <boost/thread.hpp> void Foo() { std::cout << "Hello world, I'm a thread!" << std::endl; } int main(int argc, char* argv[]) { //以下两种写法都可以 //boost::thread thrd(&Foo); boost::thread thrd(Foo); thrd.join(); return 0; }
使用对象实例创建线程
#include "stdafx.h" #include <iostream> #include <boost/thread.hpp> boost::mutex g_io_mutex; class CTestClass { public: CTestClass(int id) : m_id(id) {} void operator()() { for(int i = 0; i < 10; ++i) { boost::mutex::scoped_lock lock(g_io_mutex); std::cout<<m_id<<": "<<i<<std::endl; } } private: int m_id; }; int main() { //创建一个临时对象 boost::thread thrd2(CTestClass(1)); CTestClass testClass_2( 2 ); //创建一个已有对象的引用 boost::thread thrd1(boost::ref( testClass_2 )); thrd1.join(); thrd2.join(); system("pause"); return 0; }
通过成员函数创建线程
#include "stdafx.h" #include <iostream> #include <boost/thread.hpp> boost::mutex g_io_mutex; class CTestClass { public: CTestClass(int id) : m_id(id) {} void operator()() { for(int i = 0; i < 10; ++i) { boost::mutex::scoped_lock lock(g_io_mutex); std::cout<<m_id<<": "<<i<<std::endl; } } void Foo() { std::cout << "Foo procedure." << std::endl; } private: int m_id; }; int main() { CTestClass testClass_1( 102 ); boost::function0<void> f = boost::bind(&CTestClass::Foo, &testClass_1); boost::thread thrd1(f); thrd1.join(); system("pause"); return 0; }
相关文章推荐
- Java中equals和==的区别
- Java NIO系列教程(7):FileChannel
- 多目标优化
- Android开发性能优化总结(二)
- MVC中Section、Partial View 和 Child Action(基本用法)
- 各种排序算法代码(C语言版)
- shell之别名和函数
- Java NIO 系列教程(6):Selector
- [剑指offer]二进制中1的个数
- 第 20 章 项目实战--首页内容介绍[上][3]
- php中实现的一个curl批处理的实例
- bzoj4538 [Hnoi2016]网络
- AmazeUI 模态框封装
- 正则的使用3
- 【Winform】.cs文件命名空间排序及注释批量处理工具
- Java NIO系列教程(5):通道之间的数据传输
- Java NIO系列教程(4):Scatter/Gather
- HTML5中的智能表单
- Hibernate 多对多关联查询条件使用
- ubuntu下配置SSH已经filezilla