0

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.

IoT
  • 607
  • 1
  • 11
  • 23
  • What's the type of 'response_received'? Also, std now has mutex and condition_variable, there's no need to use Boost – James Mar 14 '14 at 13:13
  • I am receiving a a response for the request I sent , and I am using boost because it has a lot of options. – IoT Mar 14 '14 at 13:31
  • Try to see if the 'while (!response_received)' loop is actually waiting on the condition_variable, or if it's spinning – James Mar 14 '14 at 14:48
  • First of all , I changed to the standard library, and then I checked the loop, it's waiting and nothing is happening. And all the threads which are created are blocked too. It seems like whenever any thread reaches this condition variable, all other created threads get blocked. So what do you think the problem is? – IoT Mar 14 '14 at 15:15
  • @James any answer from you side? – IoT Mar 14 '14 at 18:19
  • Where do you alter the state? It looks like you're setting the state back to `SRC_WAIT_HO_COMPLETE_INDICATION`, so I'd assume that the code will loop directly back to `wait_ho_complete_indication_state:`. There's no telling whether this is intended from the code here, but it looks suspect to me. – sehe Mar 15 '14 at 16:17
  • @sehe whenever the state machine enter a new state I define it as the current state, in my case `mState = SRC_WAIT_HO_COMPLETE_INDICATION;` Anyone the newly created threads reach first to this state, it will freeze everything. Also you asking for more, which can be found here, I have included everything related to the creation of the threads and the mutex http://stackoverflow.com/questions/22417656/boost-1-49-condition-variable-issue – IoT Mar 15 '14 at 16:31

0 Answers0