[英]Is it necessary to lock?
我在leetcode中遇到了一个问题。 我已经在讨论中查看了一些解决方案。 但是我的解决方案与其他解决方案不同,因为我没有first
在方法中使用lock
。 我想知道我的代码是否正确。 此外,您能给我一些有关我的代码的建议吗?
我认为没有必要像void second(function<void()> printSecond)
unique_lock
在方法void first(function<void()> printFirst)
void second(function<void()> printSecond)
,对吗?
class Foo {
public:
Foo() {
}
void first(function<void()> printFirst) {
// cout<<1<<endl;
// printFirst() outputs "first". Do not change or remove this line.
// mtx.lock();
printFirst();
flag=1;
// mtx.unlock();
cond.notify_all();
// cout<<11<<endl;
}
void second(function<void()> printSecond) {
// cout<<2<<endl;
{
unique_lock<mutex> lk(mtx);
cond.wait(lk,[this](){return flag==1;});
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
flag=2;
}
// cout<<22<<endl;
cond.notify_all();
}
void third(function<void()> printThird) {
// cout<<3<<endl;
unique_lock<mutex> lk(mtx);
cond.wait(lk,[this](){return flag==2;});
// printThird() outputs "third". Do not change or remove this line.
printThird();
flag=3;
// cout<<33<<endl;
}
mutex mtx;
condition_variable cond;
int flag=0;
};
显然,您的三个元素函数应该由不同的线程调用。 因此,您需要在每个线程中锁定互斥对象,以保护公共变量flag
不被并发访问。 所以,你应该取消注释mtx.lock()
和mtx.unlock()
在first
以保护它有作为。 second
和third
函数应用unique_lock
作为替代方法。
始终确保调用之前解锁互斥cond.notify_all()
通过调用mtx.unlock()
之前或者使unique_lock
内码块的局部变量作为second
。
在类定义底部的元素变量之前放置一个private:
:,以防止它们受到外部访问。 这将确保在不锁定互斥锁的情况下不能更改flag
。
有必要。
在这种情况下,您的代码产生正确的输出是很重要的。 这是完全有可能的是printFirst
可能不是由时间完成printSecond
被调用。 您需要互斥体来防止这种情况,并停止printSecond
和printThird
。 避免同时运行。
当first()
将1
分配给flag
时,可以同时评估second()
或third()
的flag
检查条件。
改写这个
cond.wait(lk, [this](){return flag==1;});
这样,可能更容易看到:
while(!(flag==1)) cond.wait(lk);
它与带有lambda的wait()
的作用相同。
flag
应该被读取,而互斥举行-但first
不约互斥和受让人照顾flag
时,它为所欲为。 对于非原子类型,这是一场灾难。 它可能工作 1000万次(可能会)-但是当事情实际上同时发生时(因为您放任)- 繁荣 -未定义的行为
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.