Boost::thread库的使用(转)
原文转自 http://blog.csdn.net/lee353086/article/details/4673790
本文主要由线程启动、Interruption机制、线程同步、等待线程退出、Thread Group几个部份组成。
1、线程启动。线程可以从以下四种方式启动:
(1) 用struct结构的operator成员函数启动
struct callable
{
void operator()() { 这里略去若干行代码 }
}; 这里略去若干行代码 Callable x;
Boost::thread t(x);
(2) 以非成员函数形式启动线程
void func(int nP)
{ 这里略去若干行代码
}
这里略去若干行代码
Boost::thread t(func,);
(3) 以成员函数形式启动线程
#include <boost/bind.hpp> class testBind
{
public:
void testFunc(int i)
{
cout << ”i = ” << i << endl;
}
}; testBind tb;
boost::thread t(boost::bind(&testBind::testFunc, &tb, ));
2、Interruption机制
可以通过thread对象的interrupt函数,通知线程,需要interrupt。线程运行到interruption point就可以退出。Interruption机制举例:
#include "stdafx.h"
#include <iostream>
#include <boost/thread.hpp>
using namespace std; void f()
{
for (int i = ; i < 0x0fffffff; i++)
{
if (i % 0xffffff == )
{
cout << "i=" << ((i & 0x0f000000) >> ) << endl;
cout << "boost::this_thread::interruption_requested()=" << boost::this_thread::interruption_requested() << endl;
if (((i & 0x0f000000) >> ) == )
{
boost::this_thread::interruption_point();
}
}
}
} int _tmain(int argc, _TCHAR* argv[])
{
boost::thread t(f);
t.interrupt();
t.join(); //等待线程结束
return ;
}
t.interrupt();告诉t线程,现在需要interrupt。boost::this_thread::interruption_requested()可以得到当前线程是否有一个interrupt请求。若有interrupt请求,线程在运行至interruption点时会结束。
boost::this_thread::interruption_point();就是一个interruption point。Interruption point有多种形式,较常用的有boost::this_thread::sleep(boost::posix_time::seconds(5));当没有interrupt请求时,这条语句会让当前线程sleep五秒,若有interrupt requirement线程结束。
如何使线程在运行到interruption point的时候,不会结束,可以参考下面的例子:
#include "stdafx.h"
#include <iostream>
#include <boost/thread.hpp>
using namespace std; void f()
{
for (int i = ; i < 0x0fffffff; i++)
{
if (i % 0xffffff == )
{
cout << "i=" << ((i & 0x0f000000) >> ) << endl; cout << "boost::this_thread::interruption_requested()" << boost::this_thread::interruption_requested() << endl; if (((i & 0x0f000000) >> ) == )
{
boost::this_thread::disable_interruption di;
{
boost::this_thread::interruption_point();
}
}
}
}
} int _tmain(int argc, _TCHAR* argv[])
{
boost::thread t(f);
t.interrupt();
t.join(); //等待线程结束 return ;
}
注意boost::this_thread::disable_interruption这条语句的使用,它可以使大括号内的interruption point不会中断当前线程。
3、线程同步
Boost提供了多种lock导致上手需要较长时间,还是看下面线程同步的例子比较简单,相信在多数应用中足够:
直接使用boost::mutex的例子
static boost::mutex g_m;
这里略去若干行代码
g_m.lock();
需要锁定的代码
g_m.unlock();
这里略去若干行代码
if (g_m.try_lock())
{
需要锁定的代码
}
这里略去若干行代码
使用lock guard的例子
#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp> using namespace std; static boost::mutex g_m; void f(string strName)
{
for (int i = ; i < 0x0fffffff; i++)
{
if (i % 0xffffff == )
{
boost::lock_guard<boost::mutex> lock(g_m);
cout << "Name=" << strName << " i=" << ((i & 0x0f000000) >> ) << endl;
}
}
} int _tmain(int argc, _TCHAR* argv[])
{
boost::thread t(f, string("inuyasha"));
boost::thread t2(f, string("kagula"));
boost::thread t3(f, string("kikyou")); {
boost::lock_guard<boost::mutex> lock(g_m);
cout << "thread id=" << t.get_id() << endl;
} t.join();
t2.join();
t3.join(); return ;
}
使用unique lock的例子
#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp> using namespace std; static boost::mutex g_m; void f(string strName)
{
cout << "Thread name is " << strName << "-----------------begin" << endl;
for (int i = ; i < 0x0fffffff; i++)
{
if (i % 0xffffff == )
{
boost::unique_lock<boost::mutex> lock(g_m); cout << "Name=" << strName << " i=" << ((i & 0x0f000000) >> ) << endl; lock.unlock();
}
}
cout << "Thread name is " << strName << "-----------------end" << endl;
} int _tmain(int argc, _TCHAR* argv[])
{
boost::thread t(f, string("inuyasha"));
boost::thread t2(f, string("kagula"));
boost::thread t3(f, string("kikyou")); t.join();
t2.join();
t3.join(); return ;
}
同Lock_guard相比
[1]Unique lock中有owns lock成员函数,可判断,当前有没有被lock。
[2]在构造Unique Lock时可以指定boost::defer_lock_t参数推迟锁定,直到Unique Lock实例调用Lock。或采用下面的编码方式使用:
boost::unique_lock<boost::mutex> lock(mut,boost::defer_lock);
boost::unique_lock<boost::mutex> lock2(mut2,boost::defer_lock);
boost::lock(lock,lock2);
[3]它可以和Conditoin_variable配合使用。
[4]提供了try lock功能。
如果线程之间执行顺序上有依赖关系,直接到boost官网中参考条件变量(Condition variables)的使用。官网关于Conditon Variables的说明还是容易看懂的。
注意,使用一个不恰当的同步可能消耗掉1/2以上的cpu运算能力。
Thread Group
线程组使用示例,其中f函数在上面的例子已经定义
int _tmain(int argc, _TCHAR* argv[])
{
boost::thread_group tg;
tg.add_thread(new boost::thread(f,string("inuyasha")));
tg.add_thread(new boost::thread(f,string("kagula")));
tg.add_thread(new boost::thread(f,string("kikyou"))); tg.join_all(); return ;
}
Boost::thread库的使用(转)的更多相关文章
- Boost::thread库的使用
阅读对象 本文假设读者有几下Skills [1]在C++中至少使用过一种多线程开发库,有Mutex和Lock的概念. [2]熟悉C++开发,在开发工具中,能够编译.设置boost::thread库. ...
- boost::thread 库的使用
转载自:http://blog.csdn.net/yockie/article/details/9181939 概要 通过实例介绍boost thread的使用方式,本文主要由线程启动.Interru ...
- Boost Thread学习笔记五
多线程编程中还有一个重要的概念:Thread Local Store(TLS,线程局部存储),在boost中,TLS也被称作TSS,Thread Specific Storage.boost::thr ...
- Boost Thread学习笔记二
除了thread,boost种:boost::mutexboost::try_mutexboost::timed_mutexboost::recursive_mutexboost::recursive ...
- Boost Thread学习笔记
thread自然是boost::thread库的主 角,但thread类的实现总体上是比较简单的,前面已经说过,thread只是一个跨平台的线程封装库,其中按照所使用的编译选项的不同,分别决定使用 W ...
- boost::thread boost库线程
一.boost::thread的创建 1.线程创建方法一: boost::shared_ptr<boost::thread> writeThread_; boost::function0& ...
- c++ boost asio库初学习
前些日子研究了一个c++的一个socket库,留下范例代码给以后自己参考. 同步server: // asio_server.cpp : コンソール アプリケーションのエントリ ポイントを定義します. ...
- 【boost】MFC dll中使用boost thread的问题
项目需要,在MFC dll中使用了boost thread(<boost/thread.hpp>),LoadLibraryEx的时候出现断言错误,去掉thread库引用后断言消失. 百度g ...
- Boost线程库学习笔记
一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...
随机推荐
- Foxmail登录不了网易企业邮箱解决办法
关于Foxmail登录不了网易企业邮箱问题 解决办法是:在设置账号的时候手动设置pop服务器和smtp服务器. 新建账号的图: 点击“手动设置”出现如下界面: 设置完成后问题解决.下面的两个是正确的, ...
- 基于python3.7的一个闯越自动签到脚本--demo版
望指正demo的定位,有时候会抽风无法接受我的定位信息 #! /usr/bin/python3 # -*- coding:UTF- -*- # time : // : # file : chuangy ...
- [译]The Python Tutorial#7. Input and Output
[译]The Python Tutorial#Input and Output Python中有多种展示程序输出的方式:数据可以以人类可读的方式打印出来,也可以输出到文件中以后使用.本章节将会详细讨论 ...
- psutil——获取系统信息的Python第三方模块
本文摘自廖雪峰大神个人网站:https://www.liaoxuefeng.com/wiki/1016959663602400/1183565811281984 用Python来编写脚本简化日常的运维 ...
- Special Segments of Permutation - CodeForces - 1156E (笛卡尔树上的启发式合并)
题意 给定一个全排列\(a\). 定义子区间\([l,r]\),当且仅当\(a_l + a_r = Max[l,r]\). 求\(a\)序列中子区间的个数. 题解 笛卡尔树上的启发式合并. \(200 ...
- [NOIP2015]运输计划(树上差分+LCA+二分)
Description 公元 2044 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n−1 条双向航道,每条航道建立在两个星球之间,这 n−1 条航道连通了 L 国的所有星球. 小 P 掌管 ...
- 笔记-python-__new__()
笔记-python-__new__() 1. __new__() __new__() 是在新式类中新出现的方法,它作用在构造方法建造实例之前. 验证代码: class Person(obj ...
- 3 ways of including JavaScript in HTML
Code written in JavaScript must be executed from a document written in HTML. There are three ways of ...
- 自定义RadioGrop,支持添加包裹着的RadioButton
控件类: package com.chinaCEB.cebView; import android.annotation.TargetApi; import android.content.Conte ...
- hadoop伪分布式集群的搭建
集群配置: jdk1.8.0_161 hadoop-2.6.1 linux系统环境:Centos6.5 创建普通用户 dummy 设置静态IP地址 Hadoop伪分布式集群搭建: 为普通用户添加su ...