Thread: Boost Threading : Mutex with Conditional Variable ,main thread hanging

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    4

    Unhappy Boost Threading : Mutex with Conditional Variable ,main thread hanging

    Hello,

    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:

    Code:
    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
    Code:
    
    
    Code:
    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.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    why are you using goto? goto is bad. always.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    Quote Originally Posted by Elkvis View Post
    why are you using goto? goto is bad. always.
    I am using goto, to move between different states of the State Machine. But my problem is why, the main thread gets hanged? Not the goto.

    Is there any problem in my conditional variable definition?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I don't see anything wrong with your initialization of the condition variable, although your goto could easily be the problem. If the goto jumps into a different function or jumps over initialization of other variables, it can cause undefined and/or unexpected behavior. You really aren't showing enough code to be helpful.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> ... additional to that the event_handler doesn't report that it has received any event.
    If the "Event_Handler function" is never run, then the condition is never signaled. I would look into why your event handler isn't being called.

    Condition usage looks fine, though I prefer to call notify_xxx while the lock is still acquired - condvars: signal with mutex locked or not? | Loïc OnStage

    Mixed use of unique_lock and lock_guard looks inconsistent.

    gg

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    @Codeplug. thanks for your reply.
    Actually what's happening is the program freezes, the main thread and all the newly created thread. I also have a separate thread for a TCP server, and it doesn't respond at all.
    The event handler is never executed in the main thread. And regarding the use of the lock_gaurd and unique_lock I am following the boost documentation exactly as following:

    Synchronization - 1.49.0

    @Elkvis. All the threads which are created and correspond to the State machine, are really working good. I don't think the problem is from the goto.

    I don't know why the whole program is freezing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threading with boost
    By baxy in forum C++ Programming
    Replies: 5
    Last Post: 03-07-2014, 05:20 AM
  2. boost::mutex error
    By nimitzhunter in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2011, 12:37 PM
  3. Thread/mutex difficulty
    By erasm in forum C Programming
    Replies: 30
    Last Post: 09-21-2009, 06:11 PM
  4. boost::mutex vs criticalsection
    By l2u in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2007, 04:30 AM
  5. Threading and mutex's
    By megatron09 in forum C++ Programming
    Replies: 14
    Last Post: 09-07-2006, 02:40 PM

Tags for this Thread