Boost logo

Boost Users :

Subject: [Boost-users] Named condition not behaving as expected
From: rudasi (rahuludasi_at_[hidden])
Date: 2013-07-18 15:12:30


I have 2 processes (producer and consumer) sharing an int deque in shared
memory, I have the producer process put 2 numbers in the deque and then it
gets in a wait state losing its mutex lock. I then have the consumer process
removing the numbers and printing them. It then does a notify on the
condition which the producer is waiting on. The consumer then goes on its
own wait on a second condition. After this case the producer does not wake
up. I am using the same mutex between the processes. I have removed some of
the code where i set up the shared memory and create the mutex and
conditions. Producer process:

int main()
{
    scoped_lock<named_mutex> lock(mutex);
    while(1)
    {
        while (MyDeque->size() == 2)
        {
            full.wait(lock);
            std::cout << "unlocked producer" << std::endl;
        }

        if (MyDeque->size() == 0)
        {
            *a = 2;
            MyDeque->push_back(a);
        }

        if (MyDeque->size() == 1)
        {
            *b = 4;
            MyDeque->push_back(b);
            empty.notify_one();
        }
    }
}
Consumer process:

int main()
{
    scoped_lock<named_mutex> lock(mutex);
    while(1)
    {

         //volatile int size = MyDeque->size();
         while (MyDeque->size() == 0)
         {
             empty.wait(lock);
         }

         if (MyDeque->size() == 2)
         {
             std::cout << "Consumer: " << *MyDeque->front() << std::endl;
             MyDeque->pop_front();
         }

         if (MyDeque->size() == 1)
         {
             std::cout << "Consumer: " << *MyDeque->front() << std::endl;
             MyDeque->pop_front();
             full.notify_one();
         }
    }
}
While debugging things seem to go ok for the first iteration, the producer
puts numbers 2 and 4 on the deque then waits on the full condition. The
Consumer then gets the lock, prints these numbers, does a notify_one on the
full condition and then goes into a wait. After this the producer does not
wake up.

--
View this message in context: http://boost.2283326.n4.nabble.com/Named-condition-not-behaving-as-expected-tp4649790.html
Sent from the Boost - Users mailing list archive at Nabble.com.

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