Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET

分类: C/C++ 2011-07-06 14:48 5926人阅读 评论(3) 收藏 举报

Boost::Thread的实现总体上是比较简单的,前面已经说过,thread只是一个跨平台的线程封装库,其中按照所使用的线程选项的不同,分别决定使用Windows线程API,pThread,或Mac平台的thread实现。以下只讨论Windows,即使用BOOST_HAS_WINTHREAD的情况。

Boost::Thread有两个构造函数:一个是thread(),构造一个表示当前执行线程的线程对象;一个是explicit thread(const boost::function0<void>& threadfunc),这里的boost::function0<void>可以简单看为一个无返回无参数的函数。这里的函数可以是类重载operator()构成的函数;该构造函数传入的是函数对象而并非是函数指针,这样一个具有一般函数特性的类也能作为参数传入,可以看下面的几个例子。

(1)最简单方法

 

  1. #include <boost/thread/thread.hpp>  
  2. #include <iostream>  
  3.   
  4. void hello()  
  5. {  
  6.     std::cout<<"Hello world, I'm a thread!"<<std::endl;  
  7. }  
  8.   
  9. int main()  
  10. {  
  11.     boost::thread thrd(&hello);  
  12.     thrd.join();  
  13.   
  14.     system("pause");  
  15.     return 0;  
  16. }  
#include <boost/thread/thread.hpp>
#include <iostream> void hello()
{
std::cout<<"Hello world, I'm a thread!"<<std::endl;
} int main()
{
boost::thread thrd(&hello);
thrd.join(); system("pause");
return 0;
}

 

(2)复杂类型对象作为参数来创建线程

 

  1. #include <boost/thread/thread.hpp>  
  2. #include <boost/thread/mutex.hpp>  
  3. #include <iostream>  
  4.   
  5. boost::mutex io_mutex;  
  6.   
  7. struct count  
  8. {  
  9.     count(int id) : id(id) {}  
  10.   
  11.     void operator()()  
  12.     {  
  13.         for(int i = 0; i < 10; ++i)  
  14.         {  
  15.             boost::mutex::scoped_lock lock(io_mutex);  
  16.             std::cout<<id<<": "<<i<<std::endl;  
  17.         }  
  18.     }  
  19.   
  20.     int id;  
  21. };  
  22.   
  23. int main()  
  24. {  
  25.     boost::thread thrd1(count(1));  
  26.     boost::thread thrd2(count(2));  
  27.     thrd1.join();  
  28.     thrd2.join();  
  29.   
  30.     system("pause");  
  31.     return 0;  
  32. }  
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream> boost::mutex io_mutex; struct count
{
count(int id) : id(id) {} void operator()()
{
for(int i = 0; i < 10; ++i)
{
boost::mutex::scoped_lock lock(io_mutex);
std::cout<<id<<": "<<i<<std::endl;
}
} int id;
}; int main()
{
boost::thread thrd1(count(1));
boost::thread thrd2(count(2));
thrd1.join();
thrd2.join(); system("pause");
return 0;
}

 

(3)在类内部创建线程

类内部静态方法启动线程

 

  1. #include <boost/thread/thread.hpp>  
  2. #include <iostream>  
  3.   
  4. class HelloWorld  
  5. {  
  6. public:  
  7.     static void hello()  
  8.     {  
  9.         std::cout<<"Hello world, I'm a thread!"<<std::endl;  
  10.     }  
  11.     static void start()  
  12.     {  
  13.         boost::thread thrd(hello);  
  14.         thrd.join();  
  15.     }  
  16. };  
  17.   
  18. int main()  
  19. {  
  20.     HelloWorld::start();  
  21.   
  22.     system("pause");  
  23.     return 0;  
  24. }  
#include <boost/thread/thread.hpp>
#include <iostream> class HelloWorld
{
public:
static void hello()
{
std::cout<<"Hello world, I'm a thread!"<<std::endl;
}
static void start()
{
boost::thread thrd(hello);
thrd.join();
}
}; int main()
{
HelloWorld::start(); system("pause");
return 0;
}

在这里,start()和hello()方法都必须是static方法。如果要求start()和hello()方法不能是静态方法则采用下面的方法创建线程:

 

 

  1. #include <boost/function/function0.hpp>  
  2. #include <boost/thread/thread.hpp>  
  3. #include <iostream>  
  4.   
  5. class HelloWorld  
  6. {  
  7. public:  
  8.     void hello()  
  9.     {  
  10.         std::cout<<"Hello world, I'm a thread!"<<std::endl;  
  11.     }  
  12.     void start()  
  13.     {  
  14.         boost::function0<void> f = boost::bind(&HelloWorld::hello, this);  
  15.         boost::thread thrd(f);  
  16.         thrd.join();  
  17.     }  
  18. };  
  19.   
  20. int main()  
  21. {  
  22.     HelloWorld hello;  
  23.     hello.start();  
  24.   
  25.     system("pause");  
  26.     return 0;  
  27. }  
#include <boost/function/function0.hpp>
#include <boost/thread/thread.hpp>
#include <iostream> class HelloWorld
{
public:
void hello()
{
std::cout<<"Hello world, I'm a thread!"<<std::endl;
}
void start()
{
boost::function0<void> f = boost::bind(&HelloWorld::hello, this);
boost::thread thrd(f);
thrd.join();
}
}; int main()
{
HelloWorld hello;
hello.start(); system("pause");
return 0;
}

(3)在Singleton模式内部创建线程:

 

  1. #include <boost/thread/thread.hpp>  
  2. #include <iostream>  
  3.   
  4. class HelloWorld  
  5. {  
  6. public:  
  7.     void hello()  
  8.     {  
  9.         std::cout<<"Hello world, I'm a thread!"<<std::endl;  
  10.     }  
  11.     static void start()  
  12.     {  
  13.         boost::thread thrd(boost::bind(&HelloWorld::hello, &HelloWorld::getInstance()));  
  14.         thrd.join();  
  15.     }  
  16.     static HelloWorld& getInstance()  
  17.     {  
  18.         if(!instance)  
  19.             instance = new HelloWorld;  
  20.         return *instance;  
  21.     }  
  22. private:  
  23.     HelloWorld() {}  
  24.     static HelloWorld* instance;  
  25. };  
  26.   
  27. HelloWorld* HelloWorld::instance = 0;  
  28. int main()  
  29. {  
  30.     HelloWorld::start();  
  31.   
  32.     system("pause");  
  33.     return 0;  
  34. }  
#include <boost/thread/thread.hpp>
#include <iostream> class HelloWorld
{
public:
void hello()
{
std::cout<<"Hello world, I'm a thread!"<<std::endl;
}
static void start()
{
boost::thread thrd(boost::bind(&HelloWorld::hello, &HelloWorld::getInstance()));
thrd.join();
}
static HelloWorld& getInstance()
{
if(!instance)
instance = new HelloWorld;
return *instance;
}
private:
HelloWorld() {}
static HelloWorld* instance;
}; HelloWorld* HelloWorld::instance = 0;
int main()
{
HelloWorld::start(); system("pause");
return 0;
}

(4)用类内部函数在类外部创建线程

 

 

  1. #include <boost/thread/thread.hpp>  
  2. #include <iostream>  
  3. #include <string>  
  4.   
  5. class HelloWorld  
  6. {  
  7. public:  
  8.     void hello(const std::string& str)  
  9.     {  
  10.         std::cout<<str<<std::endl;  
  11.     }  
  12. };  
  13.   
  14. int main()  
  15. {  
  16.     HelloWorld obj;  
  17.     boost::thread thrd(boost::bind(&HelloWorld::hello, &obj, "Hello World, I'm a thread!"));  
  18.     thrd.join();  
  19.   
  20.     system("pause");  
  21.     return 0;  
  22. }  
#include <boost/thread/thread.hpp>
#include <iostream>
#include <string> class HelloWorld
{
public:
void hello(const std::string& str)
{
std::cout<<str<<std::endl;
}
}; int main()
{
HelloWorld obj;
boost::thread thrd(boost::bind(&HelloWorld::hello, &obj, "Hello World, I'm a thread!"));
thrd.join(); system("pause");
return 0;
}

 

  1. 如果需要绑定的函数有参数则需要使用boost::bind。比如想使用boost::thread创建一个线程来执行函数void f(int i),如果这样写boost::thread thrd(f)是不对的,因为thread构造函数声明接受的是一个没有参数且返回类型为void的函数,而且不提供参数f也无法运行,这时就可以写boost::thread thrd(boost::bind(f, 1))。涉及到有参函数的绑定问题基本上都是boost::thread、boost::function、boost::bind结合起来使用。  
如果需要绑定的函数有参数则需要使用boost::bind。比如想使用boost::thread创建一个线程来执行函数void f(int i),如果这样写boost::thread thrd(f)是不对的,因为thread构造函数声明接受的是一个没有参数且返回类型为void的函数,而且不提供参数f也无法运行,这时就可以写boost::thread thrd(boost::bind(f, 1))。涉及到有参函数的绑定问题基本上都是boost::thread、boost::function、boost::bind结合起来使用。

参考:

 

 

http://www.cnblogs.com/VRS_technology/archive/2010/09/15/1826812.html

http://www.blogjava.net/LittleDS/archive/2008/05/18/201236.html

 

 

 

Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET的更多相关文章

  1. boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET

    boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET     boost::crc_32_type crc32;       crc32. ...

  2. boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET

    boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET boost:regex分割字符串(带有'\'字符) 分类: C++ 2011-08- ...

  3. Mybatis 示例之 Association - 偶尔记一下 - 博客频道 - CSDN.NET

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  4. 让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET

    让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET 让 QtWebkit 支持跨域CROS 2013-05-23 22:05 450人阅读 评论 ...

  5. Cannot call sendError() after the response has been committed - baiyangliu - 博客频道 - CSDN.NET

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  6. CUDA从入门到精通 - Augusdi的专栏 - 博客频道 - CSDN.NET

    http://blog.csdn.net/augusdi/article/details/12833235 CUDA从入门到精通 - Augusdi的专栏 - 博客频道 - CSDN.NET CUDA ...

  7. 最牛B的编码套路 - 呦呦鹿鸣 - 博客频道 - CSDN.NET

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  8. java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET

    java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了!      社区福利快来领取免费参加MDCC大会机会哦    Tag功能介绍—我们 ...

  9. Notepad++前端开发常用插件介绍 - BorisHuai前端修炼 - 博客频道 - CSDN

    Notepad++前端开发常用插件介绍 - BorisHuai前端修炼 - 博客频道 - CSDN.NET http://blog.csdn.net/borishuai/article/details ...

随机推荐

  1. Deep Learning for Natural Language Processing1

    Focus, Follow, and Forward Stanford CS224d 课程笔记 Lecture1 Stanford CS224d 课程笔记 Lecture1 Stanford大学在20 ...

  2. undo_retention:确定最优的撤销保留时间

    使用下面的公式来计算undo_retention参数的值: undo_retention=undo size/(db_block_size * undo_block_per_sec) 可以通过提交下面 ...

  3. 如何修改被编译后DLL文件

    原文 http://www.cnblogs.com/wujy/p/3275855.html 我们平时在工作中经常会遇到一些已经被编译后的DLL,而且更加麻烦是没有源代码可以进行修改,只能针对这个DLL ...

  4. 本地拦截genymotion或者Android模拟器的网络请求

    我们在主机上面运行了Burp或者fiddler,那么代理已经监听在本机的8080端口了. 那么我们需要在模拟器中进行如下设置: 1.在设置中,长按当前连接的wifi网络,弹出如下: 2. 点击修改网络 ...

  5. jQuery 中的 Ajax $.ajax() load() $.get() $.post() $.getJSON() $.getScript()

    1. $.ajax()方法 参数对象属性如下: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type String (默认: "GET") ...

  6. C语言入门(16)——C语言的数组

    和结构体类似,数组也是一种复合数据类型,它由一系列相同类型的元素组成.C语言支持一维数组和多维数组.如果一个数组的所有元素都不是数组,那么该数组称为一维数组. 一维数组的定义方式 在C语言中使用数组必 ...

  7. error LNK2019: 无法解析的外部符号

    前些日子电脑系统崩了,重装了了下,原有的项目环境得重新搭建,总是在链接时提示:error LNK2019: 无法解析的外部符号………… 起初以为是库没包含全,可发现不是 有想了下可能是库的包含次序有问 ...

  8. stdin、stdout、stderr

    1 ferror 2 stdin 3 stdout 4 stderr 1 ferror 功能:检测文件是否出现错误 返值:未出错0,出错非0 说明:每次调用文件输入输出函数,均产生一个新的ferror ...

  9. 使用yii中CSecurityManager的一点小技巧

    当我们使用CSecurityManager::encrypt对字符串进行加密, 加密后的字符串是一串乱码(看起来确实像乱码, 具体是什么有待考证), 这不利于我们的下一步操作. 我们可以使用base6 ...

  10. CRM setValue方法日期类型字段赋值

    setvalue datetime 赋值 得到“/Date(14000023232323)/”  这样的值 需要把 /去掉 var dd = Result.yt_purchase_date.subst ...