boost :: interprocess :: interprocess_condition :: wait在等待时不会自动解锁互斥锁

时间:2013-06-13 15:03:29

标签: c++ boost-interprocess

正如当前帖子标题所说的那样,boost boost :: interprocess :: interprocess_condition :: wait假设在等待时自动解锁互斥锁,但它没有。

在以下代码中:

boost::interprocess::scoped_lock< boost::interprocess::interprocess_mutex > state_access_lock(impl->state->state_access_mut);
impl->state->state_access_cond.wait(state_access_lock);

在VS2010进入调试模式时,我按下暂停,当我看到state_access_lock在等待时仍然被锁定时感到很惊讶。

但这不是助推文件所说的here

有人有建议吗?

感谢。

2 个答案:

答案 0 :(得分:0)

根据到目前为止的评论,我想我可以推断出答案。

不要相信您传递给interprocess_condition :: wait()的scoped_lock的成员。 interprocess_condition的契约(与interprocess_condition_any不同)表明只能将其与interprocess_mutex的锁一起使用。知道这一点后,条件变量会将内部互斥锁从锁中拉出来,以便比它对锁定一无所知更有效地完成工作。

因此,在解锁互斥锁时,它不会在scoped_lock上调用unlock(),而是直接在互斥锁上调用。这适用于内部实施;不要在家里这样做。如果在锁定超出范围之前没有重新锁定互斥锁,则会发生错误。

换句话说,您在调试器中看到的行为并不表示存在问题。如果你有死锁,它必须在其他地方。

修改

给出的实际代码中的条件变量看起来很好。我发现与start_mut的交互有点奇怪。你确定那部分没有问题吗?

答案 1 :(得分:0)

好的,这是第一个帖子:

void CSharedMemory::start(start_mode mode)
{
bool start_mut_locked = true;
impl->running = true;
impl->mode = mode;

stateMetaStruct* state = impl->proc_state;

boost::interprocess::scoped_lock< boost::interprocess::interprocess_mutex > state_access_lock(state->state_access_mut);

while(impl->running)
{
    state->data_written = false;
    while(!state->data_written)
    {
        if(start_mut_locked)
        {
            // We can now unlock and let other threads to send data.
            impl->start_mut.unlock();
            start_mut_locked = false;
        }

        state->state_access_cond.wait(state_access_lock); // wait here upon sharedmemory's state change
        boost::interprocess::offset_ptr< stateMetaStruct > s = impl->shm_obj.find< stateMetaStruct >(boost::interprocess::unique_instance).first;
        state = s.get();

        if(!state->data_written)
        {
            // Spurious wakeup.
            glm_debug("Spurious wakeup.");
        }

        if(this == state->data_written_by_proccess)
        {
            state->data_written = false;
            glm_debug("Ignoring my proper event.");
        }
    }

    if(impl->running)
    {
        // Got action from other process.
        const interprocess_actions state_action = state->action;

        if(DO_STOP == state_action) {
        }
        else if(DUMP_USERS_REQUEST == state_action) {
            impl->stateChangedListener->onDumpUsersRequest();
        }
        else if(DUMP_USERS_REPLY == state_action) {
        }
        else {
            glm_err("Unexpected state.");
        }
    }
   }
}

第二个线程尝试使用此方法发送数据:

void CSharedMemory::sendDumpUsersRequest()
{
// Ensure shm is started.
boost::mutex::scoped_lock lk(impl->start_mut);

glm_debug("%s", __FUNCTION__);

boost::interprocess::offset_ptr< stateMetaStruct > s = impl->shm_obj.find< stateMetaStruct >(boost::interprocess::unique_instance).first;
stateMetaStruct* state = s.get();

boost::interprocess::scoped_lock< boost::interprocess::interprocess_mutex > state_access_lock(state->state_access_mut);

state->action = DUMP_USERS_REQUEST;

state->data_written = true;
state->data_written_by_proccess = this;

// Send request.
state->state_access_cond.notify_all();
}

行为是,第二个线程阻塞尝试获取scoped_mutex因为第一个正在等待它。