繁体   English   中英

是否需要锁定?

[英]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以保护它有作为。 secondthird函数应用unique_lock作为替代方法。

始终确保调用之前解锁互斥cond.notify_all()通过调用mtx.unlock()之前或者使unique_lock内码块的局部变量作为second

进一步的建议

在类定义底部的元素变量之前放置一个private: :,以防止它们受到外部访问。 这将确保在不锁定互斥锁的情况下不能更改flag

有必要。

在这种情况下,您的代码产生正确的输出是很重要的。 这是完全有可能的是printFirst可能不是由时间完成printSecond被调用。 您需要互斥体来防止这种情况,并停止printSecondprintThird 避免同时运行。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM