Boost Threading : Mutex with Conditional Variable

2019-09-07 00:45发布

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.

0条回答
登录 后发表回答