Boost logo

Boost Users :

Subject: [Boost-users] Condition Variable Notify
From: Matt Fair (matt.fair_at_[hidden])
Date: 2012-03-22 14:51:39


Hello, I'm using a condition variable to notify when I get an ack
message back. For the most part it is working like it is suppose to,
but I have a situation where it doesn't work. Luckly the problem is
repeatable and seems to be specific to my condition variable being
notified, I've included a simplified version of the code, are there
any outstanding issues? I have two threads, waitForAck() is called in
one and receiveAck() is called in another.

class Foo
{
  public:
    void waitForAck();
    void receiveAck();
  private:
     boost::mutex m_ackMutex;
     boost::condition_variable m_ackCond;
     bool m_receivedAck;
};

void Foo::waitForAck()
{
  boost::mutex::scoped_lock lock(m_ackMutex);
  m_receivedAck=false;

  //wait 8 seconds for ack
  boost::system_time const
waitingTime=boost::get_system_time()+boost::posix_time::seconds(8);
  do
  {

    if(m_receivedAck)
    {
        //got ack, break out of loop
        break;
    }

    //Waiting for ack...
  } while (m_ackCond.timed_wait(lock, waitingTime));

  if(!m_receivedAck)
  {
    //wait for ack timed out
  }
}

void Foo::receiveAck()
{
      boost::mutex::scoped_lock lock(m_ackMutex);
      m_receivedAck = true;
      lock.unlock();
      m_ackCond.notify_one();
 }

So for most of the time, this works as expected. However, at a
particular part of my code waitForAck is called, then receiveAck is
called in the other thread, notify is sent out but for some reason
even though i'm waiting in the timed_wait in thread 1 it waits for 8
seconds and then says there was a timeout.

So my questions are:
 1. Does anything look off, am I using the condition variable and
mutex correctly?
 2. In waitForAck, I have a scope_lock that is locked, when I enter
into timed_wait, does this unlock the mutex and the re-locks it when
m_ackCond.notify_one() is called?
 3. Are there any special circumstances with condition variable
(besides receive happens before wait) where this code shouldn't behave
as it should?

Thanks for your help, I've been banging my head against the wall here with this!
Matt


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net