Boost Threading : Mutex with Conditional Variable ,main thread hanging

烈酒焚心 提交于 2019-12-25 08:15:40

问题


I am trying to implement a state machine as a part of a class called" Source Transaction". Evey-time I receive a request in the main thread, it generates an instance of this class and the state machine starts executing until it reaches a state where it has to wait for a response in the main thread in the "event_handler". To solve this issue, I am implementing a conditional variable using boost libraries as following:

Source Transaction Class

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;
}

And in the main file I have the following:

Main Class

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

My problem is, every-time I execute the code, the state machine gets trapped in the "wait_ho_complete_indication_state" and it doesn't leave that state ,and additional to that the event_handler doesn't report that it has received any event. Like the main is being hanged.

So my question is there any problem in the implementation of the conditional variable?

Thanks a lot.

来源:https://stackoverflow.com/questions/22401535/boost-threading-mutex-with-conditional-variable-main-thread-hanging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!