Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET
Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET
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)最简单方法
- #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;
- }
#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)复杂类型对象作为参数来创建线程
- #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;
- }
#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)在类内部创建线程
类内部静态方法启动线程
- #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;
- }
#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()方法不能是静态方法则采用下面的方法创建线程:
- #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;
- }
#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模式内部创建线程:
- #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;
- }
#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)用类内部函数在类外部创建线程
- #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;
- }
#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;
}
- 如果需要绑定的函数有参数则需要使用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的更多相关文章
- boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET
boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET boost::crc_32_type crc32; crc32. ...
- boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET
boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET boost:regex分割字符串(带有'\'字符) 分类: C++ 2011-08- ...
- Mybatis 示例之 Association - 偶尔记一下 - 博客频道 - CSDN.NET
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- 让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET
让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET 让 QtWebkit 支持跨域CROS 2013-05-23 22:05 450人阅读 评论 ...
- Cannot call sendError() after the response has been committed - baiyangliu - 博客频道 - CSDN.NET
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- CUDA从入门到精通 - Augusdi的专栏 - 博客频道 - CSDN.NET
http://blog.csdn.net/augusdi/article/details/12833235 CUDA从入门到精通 - Augusdi的专栏 - 博客频道 - CSDN.NET CUDA ...
- 最牛B的编码套路 - 呦呦鹿鸣 - 博客频道 - CSDN.NET
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET
java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了! 社区福利快来领取免费参加MDCC大会机会哦 Tag功能介绍—我们 ...
- Notepad++前端开发常用插件介绍 - BorisHuai前端修炼 - 博客频道 - CSDN
Notepad++前端开发常用插件介绍 - BorisHuai前端修炼 - 博客频道 - CSDN.NET http://blog.csdn.net/borishuai/article/details ...
随机推荐
- Node.js包管理器:
Node.js包管理器: 当我们要把某个包作为工程运行的一部分时,通过本地模式获取,如果要在命令行下使用,则使用全局模式安装 使用全局模式安装的包并不能直接在JavaScript文件中用require ...
- Drawable类及XMLDrawable的使用
一.性质 可直接使用.png..jpg..gif.9.png等图片作为资源,也可使用多种XML文件作为资源.(就是这些资源都能生成Drawable对象).并对XML文件作出相关处理 二.XMLDraw ...
- web2py官方文档翻译01
第一章:介绍 介绍 web2py(web2py)是一个免费的开源web框架的敏捷开发安全的数据库驱动的web应用程序,这是用Python编写的Python(Python)和可编程.web2py是一个完 ...
- [Python]从豆瓣电影批量获取看过这部电影的用户列表
前言 由于之后要做一个实验,需要用到大量豆瓣用户的电影数据,因此想到了从豆瓣电影的“看过这部电影 的豆瓣成员”页面上来获取较为活跃的豆瓣电影用户. 链接分析 这是看过"模仿游戏"的 ...
- [Node.js]expressjs简单测试连接mysql
下载好node.js和通过npm安装好express.js后,先写package.json { "name": "application-name", &quo ...
- python----iter\next
1.说明:__getitem__\setitem可以迭代,它已经不被推荐了:建议使用__iter__\next. 2.python会先去检查__iter__\next然后再去检查__getitem__ ...
- 实现水电气一卡通 IC卡扇区分配
现在市面上我们接触到的IC卡大部分都是分为16个扇区,分别标注为0-15区.而实现一卡通的秘密就在这16个扇区之中,一个,或者一类功能用途读取这一个扇区,除了一般默认0扇区不用外,其他扇区可以被分别加 ...
- Oracle字符编码
.检查服务器编码: 执行SQL语法: Java代码 select * from v$nls_parameters; 或 Java代码 select * from nls_database_parame ...
- service:jmx:rmi:///jndi/rmi
service:jmx:rmi:///jndi/rmi://ip:9889/jmxrmi http://stackoverflow.com/questions/2768087/explain-jmx- ...
- mysql_healthly
cat mysql_healthly.php <?php if (!defined('IN_PDK')){ define('IN_PDK', true); } $db_name = $_GET[ ...