为什么我必须有一个 boost::mutex for boost::condition_variable

why must I have a boost::mutex for boost::condition_variable?

本文关键字:boost for condition variable mutex 有一个 为什么      更新时间:2023-10-16

我有以下代码:其中m_event是提升::condition_variable

        boost::scoped_lock dummy;
        boost::unique_lock<boost::scoped_lock> lock(dummy); // TODO: see if dummy is correct
        m_event.wait(lock, [this] () {
            return !this ->m_enqueue.empty();
        });

真的不需要那个虚拟储物柜,我只是希望事件在某个布尔条件下停止,我不明白什么吗?

为什么我被迫使用假锁?

(附言代码效果很好。

编辑:事实上,如果我理解正确的话,我班上的是一个 boost::mutex,我们称之为m_mtx和明显的m_enqueue插入器。所以我正在根据锁更改我的实现以锁定m_lock然后机械臂只会做:

boost::mutex::scoped_lock<boost::mutex> guard(m_lock);

有意义?

timed_wait等待获得锁,如果它及时这样做的话。

没有锁,它什么都做不了。

在我看来,你在滥用timed_wait.