Boost Thread学习笔记四

barrier
barrier类的接口定义如下:

 1 class barrier : private boost::noncopyable   // Exposition only
 2 {
 3 public:
 4   // construct/copy/destruct
 5   barrier(size_t n);
 6   ~barrier();
 7 
 8   // waiting
 9   bool wait();
10 };

barrier类为我们提供了这样一种控制线程同步的机制:
前n - 1次调用wait函数将被阻塞,直到第n次调用wait函数,而此后第n + 1次到第2n - 1次调用wait也会被阻塞,直到第2n次调用,依次类推。
barrier::wait的实现十分简单:

 1 barrier::barrier(unsigned int count)
 2     : m_threshold(count), m_count(count), m_generation(0)
 3 {
 4     if (count == 0)
 5         throw std::invalid_argument("count cannot be zero.");
 6 }
 7 
 8 bool barrier::wait()
 9 {
10     boost::mutex::scoped_lock lock(m_mutex);    // m_mutex is the base of barrier and is initilized by it's default constructor.
11     unsigned int gen = m_generation;    // m_generation will be 0 for call 1~n-1, and 1 for n~2n - 1, and so on
12 
13     if (--m_count == 0)
14     {
15         m_generation++;    // cause m_generation to be changed in call n/2n/
16         m_count = m_threshold;    // reset count
17         m_cond.notify_all();    // wake up all thread waiting here
18         return true;
19     }
20 
21     while (gen == m_generation)    // if m_generation is not changed, lock current thread.
22         m_cond.wait(lock);
23     return false;
24 }

因此,说白了也不过是mutex的一个简单应用。
以下是一个使用barrier的例子:

 1 #include <boost/thread/thread.hpp>
 2 #include <boost/thread/barrier.hpp>
 3 
 4 int i = 0;
 5 boost::barrier barr(3);    // call barr.wait 3 * n times will release all threads in waiting
 6 
 7 void thread()
 8 {
 9     ++i;
10     barr.wait();
11 }
12 
13 int main()
14 {
15     boost::thread thrd1(&thread);
16     boost::thread thrd2(&thread);
17     boost::thread thrd3(&thread);
18 
19     thrd1.join();
20     thrd2.join();
21     thrd3.join();
22 
23     return 0;
24 }

如果去掉其中thrd3相关的代码,将使得线程1、2一直处于wait状态,进而使得主线程无法退出。

xtime
xtime是boost::thread中用来表示时间的一个辅助类,它是一个仅包含两个成员变量的结构体:

1 struct xtime
2 {
3 //
4     xtime_sec_t sec;
5     xtime_nsec_t nsec;
6 };

condition::timed_wait、thread::sleep等涉及超时的函数需要用到xtime。
需要注意的是,xtime表示的不是一个时间间隔,而是一个时间点,因此使用起来很不方便。为了方便使用xtime,boost提供了一些辅助的xtime操作函数,如xtime_get、xtime_cmp等。
以下是一个使用xtime来执行sleep的例子(跟简单的一句Sleep比起来,实在是太复杂了),其中用到了xtime初始化函数xtime_get:

 1 #include <boost/thread/thread.hpp>
 2 #include <boost/thread/xtime.hpp>
 3 #include <iostream>
 4 
 5 int main()
 6 {
 7     boost::xtime xt;
 8     boost::xtime_get(&xt, boost::TIME_UTC);    // initialize xt with current time
 9     xt.sec += 1;    // change xt to next second
10     boost::thread::sleep(xt);    // do sleep
11 
12     std::cout << "1 second sleep over." << std::endl;
13 
14     return 0;
15 } 

时间: 2024-08-26 13:10:29

Boost Thread学习笔记四的相关文章

Boost Thread学习笔记二

除了thread,boost::thread另一个重要组成部分是mutex,以及工作在mutex上的boost::mutex::scoped_lock.condition和barrier,这些都是为实现线程同步提供的. mutexboost提供的mutex有6种:boost::mutexboost::try_mutexboost::timed_mutexboost::recursive_mutexboost::recursive_try_mutexboost::recursive_timed_m

Boost Thread学习笔记三

下面先对condition_impl进行简要分析.condition_impl在其构造函数中会创建两个Semaphore(信号量):m_gate.m_queue,及一个Mutex(互斥体,跟boost::mutex类似,但boost::mutex是基于CriticalSection<临界区>的):m_mutex,其中:m_queue相当于当前所有等待线程的等待队列,构造函数中调用CreateSemaphore来创建Semaphore时,lMaximumCount参数被指定为(std::nume

Boost Thread学习笔记五

多线程编程中还有一个重要的概念:Thread Local Store(TLS,线程局部存储),在boost中,TLS也被称作TSS,Thread Specific Storage.boost::thread库为我们提供了一个接口简单的TLS的面向对象的封装,以下是tss类的接口定义: class tss{public:    tss(boost::function1<void, void*>* pcleanup);    void* get() const;    void set(void*

kvm虚拟化学习笔记(四)之kvm虚拟机日常管理与配置

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://koumm.blog.51cto.com/703525/1290269 KVM虚拟化学习笔记系列文章列表 ---------------------------------------- kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51cto.com/703525/1288795 kvm虚拟化学习笔记(二)之linux kvm虚拟机安装 h

Caliburn.Micro学习笔记(四)----IHandle&lt;T&gt;实现多语言功能

Caliburn.Micro学习笔记目录 说一下IHandle<T>实现多语言功能 因为Caliburn.Micro是基于MvvM的UI与codebehind分离, binding可以是双向的所以我们想动态的实现多语言切换很是方便今天我做一个小demo给大家提供一个思路 先看一下效果                                           点击英文  变成英文状态点chinese就会变成中文                         源码的下载地址在文章的最下

Sqlite学习笔记(四)&amp;&amp;SQLite-WAL原理(转)

Sqlite学习笔记(三)&&WAL性能测试中列出了几种典型场景下WAL的性能数据,了解到WAL确实有性能优势,这篇文章将会详细分析WAL的原理,做到知其然,更要知其所以然. WAL是什么       WAL(Write ahead logging)是一种日志模式,它是一种思想,普遍应用于关系型数据库.每个事务执行变更时,修改数据页,同时会产生日志,这样在事务提交后,不需要将修改的脏页刷盘,只需要将事务产生的日志落盘即可返回.WAL保证日志一定先于对应的脏页落盘,就是所谓的WAL.SQLI

JSP学习笔记(四)-----JSP动作的使用

js|笔记 1. 该实例主要告诉我们怎么样在JSP代码中使用JSP动作,例如<jsp:forward >2. 该实例需要四个文件:login.jsp,test.jsp,ok.htm,no.htm3. 首先看一下login.jsp<html><center><form method=get action="http://127.0.0.1:8000/test.jsp">username<input type=text name=use

boost uuid 学习笔记

#include <vector>#include <iostream>#include <boost/uuid/uuid.hpp>#include <boost/uuid/uuid_generators.hpp>#include <boost/uuid/uuid_io.hpp>using namespace boost::uuids;using namespace std;int main(){ //----------------------

【Android平台】 Alljoyn学习笔记四 Android Core API参考

CORE API GUIDE - ANDROID Prerequisites Install dependencies for the Windows platform, or for the Linux platform. A device running Android OS version 2.2 (Froyo) or greater and running a chip based on the ARM 5 (or greater) instruction set. Importing