使用boost::thread跨平台多线程 (二) 线程同步-互斥
作者: 江淼
Blog: http://blog.csdn.net/jiangfriend
时间: 2007-9-20 18:19
在线程同步中有一个很重要元素,互斥体-Mutex,但互斥往往因设计上的疏乎而陷入了死锁。
在boost中同样提供了各种mutex,并尽可能减少这种死锁的疏乎。
首先看一下如下未使用原始的例子
因stream的原因本应输出的 Hello/nWorld/n 部分变成了//例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;
}
helloworld要如何才能解决这个问题?那就是mutex。
helloworld
world Run::~Run
hello Run::~Run
先认识一下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进行部分改进。
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);
}
}
运行后,意料之中,正常的输出
hello2. try_mutex+scoped_try_lock
world
world
hello
world
world Run::~Run
hello Run::~Run
void loop(void)3. timed_mutex+scoped_timed_mutex
{
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);
}
}
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)以上就是boost::mutex的用法。
{
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官方网站 http://www.boost.orgboost::thread 帮助文档 http://www.boost.org/doc/html/thread.html