thread自然是boost::thread库的主 角,但thread类的实现总体上是比较简单的,前面已经说过,thread只是一个跨平台的线程封装库,其中按照所使用的编译选项的不同,分别决定使用 Windows线程API还是pthread,或者Macintosh Carbon平台的thread实现。以下只讨论Windows,即使用 BOOST_HAS_WINTHREADS的情况。
thread类提供了两种构造函数:
thread::thread()
thread::thread(const function0<void>& threadfunc)
第 一种构造函数用于调用GetCurrentThread构造一个当前线程的thread对象,第二种则通过传入一个函数或者一个functor来创建一个 新的线程。第二种情况下,thread类在其构造函数中间接调用CreateThread来创建线程,并将线程句柄保存到成员变量m_thread中,并 执行传入的函数,或执行functor的operator ()方法来启动工作线程。

我们可以用以下三种方式启动一个新线程:
1、传递一个工作函数来构造一个工作线程

 1 #include <boost/thread/thread.hpp>
 2 #include <boost/thread/mutex.hpp>
 3 #include <iostream>
 4 
 5 boost::mutex io_mutex;
 6 
 7 void count()    // worker function
 8 {
 9     for (int i = 0; i < 10; ++i)
10     {
11         boost::mutex::scoped_lock lock(io_mutex);
12         std::cout << i << std::endl;
13     }
14 }
15 
16 int main(int argc, char* argv[])
17 {
18     boost::thread thrd1(&count);
19     boost::thread thrd2(&count);
20     thrd1.join();
21     thrd2.join();
22 
23     return 0;
24 }
25 

、传递一个functor对象来构造一个工作线程

 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);        // lock io, will be explained soon.
16             std::cout << id << ": " << i << std::endl;
17         }
18     }
19 
20     int id;
21 };
22 
23 int main(int argc, char* argv[])
24 {
25     boost::thread thrd1(count(1));
26     boost::thread thrd2(count(2));
27     thrd1.join();
28     thrd2.join();
29     return 0;
30 }
31 

、无需将类设计成一个functor,借助bind来构造functor对象以创建工作线程

 1 #include <boost/thread/thread.hpp>
 2 #include <boost/thread/mutex.hpp>
 3 #include <boost/bind.hpp>
 4 #include <iostream>
 5 
 6 boost::mutex io_mutex;
 7 
 8 struct count
 9 {
10     static int num;
11     int id;
12 
13     count() : id(num++) {}
14 
15     int do_count(int n)
16     {
17         for (int i = 0; i < n; ++i)
18         {
19             boost::mutex::scoped_lock lock(io_mutex);
20             std::cout << id << ": " << i << std::endl;
21         }
22         return id;
23     }
24 };
25 
26 int count::num = 1;
27 
28 int main(int argc, char* argv[])
29 {
30     count c1;
31     boost::thread thrd1(boost::bind(&count::do_count, &c1, 10));
32     thrd1.join();
33     return 0;
34 }

其中bind是一个函数模板,它可以根据后面的实例化参数构造出一个functor来,上面的boost中是一样的了。

Boost Thread学习笔记的更多相关文章

  1. Boost Thread学习笔记五

    多线程编程中还有一个重要的概念:Thread Local Store(TLS,线程局部存储),在boost中,TLS也被称作TSS,Thread Specific Storage.boost::thr ...

  2. Boost Thread学习笔记四

    barrierbarrier类的接口定义如下:  1 class barrier : private boost::noncopyable   // Exposition only 2 { 3 pub ...

  3. Boost Thread学习笔记三

    下面先对condition_impl进行简要分析.condition_impl在其构造函数中会创建两个Semaphore(信号量):m_gate.m_queue,及一个Mutex(互斥体,跟boost ...

  4. Boost Thread学习笔记二

    除了thread,boost种:boost::mutexboost::try_mutexboost::timed_mutexboost::recursive_mutexboost::recursive ...

  5. Boost 线程学习笔记

    Bolg转载自:http://www.cnblogs.com/lvdongjie/p/4447193.html 一: 创建线程 #include <iostream> #include & ...

  6. boost.asio学习笔记一、linux下boost库的安装

    欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/46761029 学习开源库第一步就是编译安装好库,然后执行成功一个 ...

  7. thread学习笔记--BackgroundWorker 类

    背景: 在 WinForms 中,有时要执行耗时的操作,比如统计某个磁盘分区的文件夹或者文件数目,如果分区很大或者文件过多的话,处理不好就会造成“假死”的情况,或者报“线程间操作无效”的异常,或者在该 ...

  8. boost uuid 学习笔记

    #include <vector>#include <iostream>#include <boost/uuid/uuid.hpp>#include <boo ...

  9. Boost Python学习笔记(四)

    你将学到什么 在Python中调用C++代码时的传参问题 基础类型 Python的字符串是常量,所以C++函数参数中的std::string &必须为const 修改源文件(main.cpp) ...

随机推荐

  1. 《算法导论》读书笔记之图论算法—Dijkstra 算法求最短路径

    自从打ACM以来也算是用Dijkstra算法来求最短路径了好久,现在就写一篇博客来介绍一下这个算法吧 :) Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的 ...

  2. 带符号的char类型取值范围为什么是-128——127

    以前经常看到带符号的char类型取值范围是-128——127,今天突然想为什么不是-127——127,-128是怎么来的? 127好理解,char类型是8位,最高位是符号位,0正1负,所以011111 ...

  3. python 字符串处理

    介绍字符串相关的:比较,截取,替换,长度,连接,反转,编码,格式化,查找,复制,大小写,分割等操作 什么是字符串 字符串 字符串或串(String)是由数字.字母.下划线组成的一串字符.一般记为 s= ...

  4. CUSPARSE 第三章 CUSPARAE索引和数据格式

    (纯属自学笔记,部分翻译,不会翻译的不翻译) 3.1 索引基本格式 该函数库支持 zero- and one-based 索引. The index base 是通过 cusparseIndexBas ...

  5. Sereja and Bottles

    http://codeforces.com/problemset/problem/315/A 题目意思是第ai的瓶子能开bi的瓶子.给你相应的数据,求无法用其他瓶子打开的数量(即需要外力). 一开始看 ...

  6. CString——Left、Right、Find、ReverseFind

    CString--Left.Right.Find.ReverseFind http://hi.baidu.com/shawmar/item/08b30afb0f32d46f3c1485ec CStri ...

  7. Ubuntu 12.04 安装JDK 8和Eclipse

    Ubuntu 12.04 下安装 JDK8 方法一:(缺点是安装时附加openjdk等大量程序并无法去除,长处是安装简单) $ sudo apt-get install eclipse 方法二:(长处 ...

  8. TFT ST7735的Netduino驱动

    好久没写关于netduino的文章了,工作忙是一方面,主要原因还是因为没解决TFT显示的问题,功夫不负有心人,在经过多轮研究后,总算在今天2013年12月15日的晚上9点解决了. 下面先介绍一下我所用 ...

  9. 你知道hover、active这四个伪类为什么要按顺序写吗

    刨根问底,你知道:hover等4个伪类为什么要按顺序排列吗 引言 :link,:visited,:hover,:active这4个伪类大家都不陌生,4个伪类要按照LvHa这个爱恨原则来排(外国友人起的 ...

  10. WOJ 1014

    #include<stdio.h> #include<stdlib.h> #include<math.h> int main() { double a[3][3]= ...