Boost Thread学习笔记

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值