Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Boost线程:带条件变量的互斥锁,主线程挂起_Linux_C++11_Boost Thread - Fatal编程技术网

Linux Boost线程:带条件变量的互斥锁,主线程挂起

Linux Boost线程:带条件变量的互斥锁,主线程挂起,linux,c++11,boost-thread,Linux,C++11,Boost Thread,我试图实现一个状态机,作为名为“源事务”的类的一部分。每当我在主线程中收到一个请求时,它就会生成这个类的一个实例,并且状态机开始执行,直到它达到一个状态,在该状态下它必须在“event_handler”的主线程中等待响应。为了解决这个问题,我使用boost库实现了一个条件变量,如下所示: 源交易类别 boost::mutex mut; boost::condition_variable cond; wait_ho_complete_indication_state: { mState

我试图实现一个状态机,作为名为“源事务”的类的一部分。每当我在主线程中收到一个请求时,它就会生成这个类的一个实例,并且状态机开始执行,直到它达到一个状态,在该状态下它必须在“event_handler”的主线程中等待响应。为了解决这个问题,我使用boost库实现了一个条件变量,如下所示:

源交易类别

boost::mutex mut;
boost::condition_variable cond;

wait_ho_complete_indication_state:
{
    mState = SRC_WAIT_HO_COMPLETE_INDICATION;

    boost::unique_lock<boost::mutex> lock(mut);
    while (!response_received)
    {
        cond.wait(lock);
    }

    response_received = false;
    goto success_state;
}
Event_Handler function:
        // Find the source transaction which corresponds to this Indication
        src_transaction_ptr t;
        tpool->find(msg.source(), mobile_id.to_string(), t);
        {
            boost::lock_guard<boost::mutex> lock(t->mut);
            t->response_received = true;
        }
        t->cond.notify_one();
        // Dome some stuff
boost::mutexmut;
boost::条件变量cond;
等待完成指示状态:
{
mState=SRC\u WAIT\u HO\u COMPLETE\u指示;
boost::唯一锁定(mut);
而(!收到响应)
{
等待(锁定);
}
收到的响应=错误;
后藤州;
}
在主文件中,我有以下内容:

主类

boost::mutex mut;
boost::condition_variable cond;

wait_ho_complete_indication_state:
{
    mState = SRC_WAIT_HO_COMPLETE_INDICATION;

    boost::unique_lock<boost::mutex> lock(mut);
    while (!response_received)
    {
        cond.wait(lock);
    }

    response_received = false;
    goto success_state;
}
Event_Handler function:
        // Find the source transaction which corresponds to this Indication
        src_transaction_ptr t;
        tpool->find(msg.source(), mobile_id.to_string(), t);
        {
            boost::lock_guard<boost::mutex> lock(t->mut);
            t->response_received = true;
        }
        t->cond.notify_one();
        // Dome some stuff
事件处理程序函数:
//查找与此指示对应的源事务
src_事务_ptr t;
tpool->find(msg.source(),mobile\u id.to\u string(),t);
{
boost::lock_guard lock(t->mut);
t->response\u received=true;
}
t->cond.notify_one();
//来点东西
我的问题是,每次执行代码时,状态机都会被困在“wait\u ho\u complete\u indication\u state”中,并且不会离开该状态,此外,事件处理程序也不会报告它已收到任何事件。就像主楼被吊死一样

那么我的问题是,在条件变量的实现中有什么问题吗


非常感谢。

您收到的“回复”类型是什么?另外,std现在有互斥锁和条件变量,没有必要使用boost。我正在接收我发送的请求的响应,我正在使用boost,因为它有很多选项。尝试查看“while(!response_received)”循环是否真的在等待条件变量,或者它是否正在旋转。首先,我改为标准库,然后我检查了循环,它正在等待,什么都没有发生。所有创建的线程也被阻塞。似乎每当任何线程达到此条件变量时,所有其他创建的线程都会被阻塞。“那么你认为问题出在哪里?”詹姆斯:你那边有什么回答吗?