使用boost::thread跨平台多线程 (二) 线程同步-互斥

使用boost::thread跨平台多线程 (二) 线程同步-互斥

作者: 江淼
Blog: http://blog.csdn.net/jiangfriend
时间: 2007-9-20 18:19

在线程同步中有一个很重要元素,互斥体-Mutex,但互斥往往因设计上的疏乎而陷入了死锁。
在boost中同样提供了各种mutex,并尽可能减少这种死锁的疏乎。 

首先看一下如下未使用原始的例子

//例2-1

#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
using namespace std;



/**
boost的time控制相对比较烦锁一点
*/
void sleep(int ms)

    {
    boost::xtime xt;
    boost::xtime_get(&xt,boost::TIME_UTC);
    xt.nsec += ms%1000*1000*1000;
    xt.sec += ms/1000;
    boost::thread::sleep(xt);
    }

struct Run
    {
    bool running;
    string str;
    Run(const string& str):running(false),str(str)
        {
        }
    void loop(void)
        {
        running=true;
        while(running)
            {
            cout<<str<<endl;
            ::sleep(250);
            }
        }
    void stop(void)
        {
        running=false;
        }
    virtual ~Run(void)
        {
        cout<<str<<" "<<__FUNCTION__<<endl;
        }
    };

int main(int argc,char* argv[])
    {
    Run hello("hello");
    Run world("world");
    boost::thread_group grp;
    grp.create_thread(boost::bind(&Run::loop,boost::ref(hello))); //bind hello.loop
    grp.create_thread(boost::bind(&Run::loop,boost::ref(world)));//bind world.loop
    string str;
    getline(cin,str);
    hello.stop(); //停止hello
    world.stop(); //停止world
    grp.join_all(); //等待所有线程确实已结束
    return 0;
    }


因stream的原因本应输出的 Hello/nWorld/n 部分变成了
helloworld

helloworld


world Run::~Run
hello Run::~Run
要如何才能解决这个问题?那就是mutex。
先认识一下boost提共的mutex

2.Mutex

头文件<boost/thread/mutex.hpp>

namespace boost {
  class mutex;
  class try_mutex;
  class timed_mutex;
}
顾名思义,三者分别为,常规mutex,尝试mutex,时间mutex
而mutex本身不能调用成员函数,而是由lock_ops进行控制.
对应现有的几个mutex导入了scoped_lock,scoped_try_lock,scoped_timed_lock.
scoped系列的特色就是析构时解锁,默认构造时加锁,这就很好的确定在某个作用域下某线程独占某段代码。

我们对例2-1 Run::loop进行部分改进。
1. mutex+scoped_lock
    void loop(void)
        {
        running=true;
        while(running)
            {
            typedef boost::mutex MUTEX;
            typedef MUTEX::scoped_lock LOCK;
            static MUTEX iomutex;
                {
                LOCK lock(iomutex);//锁定mutex
                cout<<str<<endl;
                }//lock析构,iomutex解锁
            ::sleep(250);
            }
        }

运行后,意料之中,正常的输出
hello
world
world
hello
world

world Run::~Run
hello Run::~Run
2. try_mutex+scoped_try_lock
    void loop(void)
        {
        running=true;
        while(running)
            {
            typedef boost::try_mutex MUTEX;
            typedef MUTEX::scoped_try_lock LOCK;
            static MUTEX iomutex;
                {
                LOCK lock(iomutex);//锁定mutex
                if(lock.locked())
                    {
                    cout<<str<<endl;
                    }
                else
                    {
                    // To do
                    boost::thread::yield(); //释放控制权
                    continue;
                    }
                }//lock析构,iomutex解锁
            ::sleep(250);
            }
        }
3. timed_mutex+scoped_timed_mutex
    void loop(void)
        {
        running=true;
        while(running)
            {
            typedef boost::timed_mutex MUTEX;
            typedef MUTEX::scoped_timed_lock LOCK;
            static MUTEX iomutex;
                {
                boost::xtime xt;
                boost::xtime_get(&xt,boost::TIME_UTC);
                xt.sec+=1;//超时时间1秒
                LOCK lock(iomutex,xt);//锁定mutex
                cout<<lock.locked()<<" "<<str<<endl;
                ::sleep(10000); //长时间
                }//lock析构,iomutex解锁
            ::sleep(250);
            }
        }
输出:
1 hello
(1秒后)
0 world //超时自动解锁且继续运行

4. mutex+lock
    void loop(void)
        {
        running=true;
        while(running)
            {
            typedef boost::mutex MUTEX;
            typedef boost::detail::thread::lock_ops<MUTEX> LOCK;
            static MUTEX iomutex;
                {
                LOCK::lock(iomutex);
                cout<<str<<endl;
                LOCK::unlock(iomutex);               
                }
            ::sleep(250);
            }
        }
以上就是boost::mutex的用法。

相关链接:

boost官方网站 http://www.boost.org
boost::thread 帮助文档 http://www.boost.org/doc/html/thread.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值