我一直boost::mutex::scoped_lock
以这种方式使用:
void ClassName::FunctionName()
{
{
boost::mutex::scoped_lock scopedLock(mutex_);
//do stuff
waitBoolean=true;
}
while(waitBoolean == true ){
sleep(1);
}
//get on with the thread's activities
}
基本上它设置waitBoolean,而另一个线程通过将waitBoolean设置为false来表示它已完成;
但是,这似乎不起作用,因为其他线程无法锁定 mutex_ !
我假设通过将 scoped_lock 括在括号中,我将终止它的锁定。不是这样吗?在线阅读说它只有在调用析构函数时才放弃互斥锁。当它超出本地范围时,它不会被销毁吗?
信号部分代码:
while(running_){
boost::mutex::scoped_lock scopedLock(mutex_);
//Run some function that need to be done...
if(waitBoolean){
waitBoolean=false;
}
}
谢谢!