C++ 为什么 Boost scoped_lock 没有解锁互斥锁?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1256240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:20:14  来源:igfitidea点击:

Why is Boost scoped_lock not unlocking the mutex?

c++multithreadingboostmutex

提问by Alex

I've been using boost::mutex::scoped_lockin this manner:

我一直在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
}

Basically it sets waitBoolean, and the other thread signals that it is done by setting waitBoolean to false;

基本上它设置waitBoolean,另一个线程通过将waitBoolean 设置为false 来表示它已完成;

This doesn't seem to work, however, because the other thread can't get a lock on mutex_ !!

然而,这似乎不起作用,因为另一个线程无法锁定 mutex_ !!

I was assuming that by wrapping the scoped_lock in brackets I would be terminating its lock. This isn't the case? Reading online says that it only gives up the mutex when the destructor is called. Won't it be destroyed when it goes out of that local scope?

我假设通过将 scoped_lock 包装在括号中,我将终止它的锁。不是这样吗?在线阅读说它只在调用析构函数时放弃互斥锁。当它超出那个本地范围时,它不会被销毁吗?

Signaling part of code:

代码的信令部分:

while(running_){
   boost::mutex::scoped_lock scopedLock(mutex_);
   //Run some function that need to be done...
   if(waitBoolean){
      waitBoolean=false;
   }
}

Thanks!

谢谢!

回答by neuro

To synchronize two threads use a condition variable. That is the state of the art way to synchronize two threads the way you want :

要同步两个线程,请使用条件变量。这是按照您想要的方式同步两个线程的最先进方法:

Using boost, the waiting part is something like :

使用 boost,等待部分是这样的:

void BoostSynchronisationPoint::waitSynchronisation()
{
    boost::unique_lock<boost::mutex> lock(_mutex);

    _synchronisationSent = false;
    while(!_synchronisationSent)
    {
        _condition.wait(lock); // unlock and wait
    }
}

The notify part is something like :

通知部分类似于:

void BoostSynchronisationPoint::sendSynchronisation()
{
    {
        boost::lock_guard<boost::mutex> lock(_mutex);
        _synchronisationSent = true;
    }

    _condition.notify_all();
}

The business with _synchronisationSent is to avoid spurrious wakeups : see wikipedia

_synchronisationSent 的目的是避免虚假唤醒:参见维基百科

回答by nos

The scoped_lock should indeed be released at the end of the scope. However you don't lock the waitBoolean when you're looping on it, suggesting you don't protect it properly other places as well - e.g. where it's set to false, and you'll end up with nasty race conditions.

scoped_lock 确实应该在作用域结束时释放。然而,当你在它上面循环时你没有锁定 waitBoolean,这表明你在其他地方也没有正确保护它 - 例如它被设置为 false 的地方,你最终会遇到令人讨厌的竞争条件。

I'd say you should use a boost::condition_variable to do this sort of things, instead of sleep + thread-unsafe checking.

我会说你应该使用 boost::condition_variable 来做这类事情,而不是睡眠 + 线程不安全检查。

回答by Gaetano Mendola

Also I would suggest to mark as volatile that waitBoolean, however you have to use a condition or even better a barrier.

另外我建议将waitBoolean标记为volatile,但是您必须使用条件甚至更好的屏障。