Boost Thread学习笔记

来源:互联网 发布:mysql存储过程示例 编辑:程序博客网 时间:2024/06/09 21:07
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
、传递一个工作函数来构造一个工作线程
#include <boost/thread/thread.hpp>#include <boost/thread/mutex.hpp>#include <iostream> boost::mutex io_mutex; void count()    // worker function{    for (int i = 0; i < 10; ++i)    {        boost::mutex::scoped_lock lock(io_mutex);        std::cout << i << std::endl;    }} int main(int argc, char* argv[]){    boost::thread thrd1(&count);    boost::thread thrd2(&count);    thrd1.join();    thrd2.join();     return 0;}


2、传递一个functor对象来构造一个工作线程
#include <iostream> boost::mutex io_mutex; struct count{    count(int id) : id(id) { }     void operator()()    {        for (int i =; i < ; ++i)        {            boost::mutex::scoped_lock lock(io_mutex);        // lock io, will be explained soon.            std::cout << id << ": " << i << std::endl;        }    }     int id;}; int main(int argc, char* argv[]){    boost::thread thrd1(count(1));    boost::thread thrd2(count(2));    thrd1.join();    thrd2.join();    return;} 
3. 无需将类设计成一个functor,借助bind来构造functor对象以创建工作线程
#include <boost/thread/thread.hpp>#include <boost/thread/mutex.hpp>#include <boost/bind.hpp>#include <iostream>boost::mutex io_mutex;struct count{    static int num;    int id;    count() : id(num++) {}    int do_count(int n)    {        for (int i = 0; i < n; ++i)        {            boost::mutex::scoped_lock lock(io_mutex);            std::cout << id << ": " << i << std::endl;        }        return id;    }};int count::num = 1;int main(int argc, char* argv[]){    count c1;    boost::thread thrd1(boost::bind(&count::do_count, &c1, 10));    thrd1.join();    return 0;}

其中bind是一个函数模板,它可以根据后面的实例化参数构造出一个functor来,上面的boost::bind(&count::do_count, &c1, 10)其实等价于返回了一个functor:
struct
 countFunctor
{

    int
 operator() ()
    {
        (&
c1)->do_count(10);    // just a hint, not actual code
    }
};

因此,以后就跟2中是一样的了。


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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 超极本没有网线接口怎么办 机动车禁令标识位置放置错误怎么办 六角头冲得不标准怎么办 滚丝杆烫手怎么办怎样能不烫手 带滚花的杆子里面脏怎么办 田螺和玉米吃了怎么办 lv包包螺丝掉了怎么办 螺狮那一段吃了怎么办 吃了不熟的田螺怎么办 包上的螺丝掉了怎么办 工厂打螺丝手疼怎么办 打螺丝打到手痛怎么办 欧曼gtl不烧尿素怎么办 放油螺丝滑牙怎么办 蝴蝶刀螺丝松了怎么办 婴儿车铆钉松了怎么办 扇子上的铆钉松怎么办 锅的把手松了怎么办 奶锅把手松了怎么办 锅的手柄烧坏了怎么办 鞋子上的铆钉生锈了怎么办 包包上的铆钉生锈了怎么办 凉鞋的铆钉生锈了怎么办 扇子的铆钉坏了怎么办 包包的铆钉坏了怎么办 汽车半轴螺丝母拧不动怎么办? 卫衣袖子短了怎么办 u型导轨蚊帐下垂怎么办 100的水管螺纹出漏水怎么办 吊顶螺丝没有防锈处理怎么办 膨胀螺丝洞松了怎么办 膨胀螺丝眼大了怎么办 墙上螺丝孔大了怎么办 膨胀螺丝孔深了怎么办 克霉膨胀栓的线怎么办 摩托车排气管螺丝断了怎么办 汽车轮胎螺丝卸不下来怎么办 内六角螺丝卸不下来怎么办 洗衣机六角螺丝卸不动怎么办 黄油嘴打不进去怎么办 螺杆冷水机氟系统有空气怎么办