I met a question in leetcode. I have viewed some solutions in the discuss. But my solution is different from others because I do not use the lock
in the method first
. I wonder whether my code is correct. Besides, can you give me some advice about my code?
I think it is not necessary to use unique_lock
in method void first(function<void()> printFirst)
like void second(function<void()> printSecond)
, is it right?
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;
};
The
flag
checking condition insecond()
orthird()
may be evaluated at the same time asfirst()
assigns1
toflag
.Rewrite this
like this and it may be easier to see:
It does the same thing as your
wait()
with a lambda.flag
is supposed to be read while the mutex is held - butfirst
does not care about mutexes and assigns toflag
whenever it pleases. For non-atomic types this is a disaster. It may work 10000000 times (and probably will) - but when things actually happen at the same time (because you let it) - boom - Undefined Behaviour