Re: [Boost-bugs] [Boost C++ Libraries] #6204: If we send a notify(cond.notify_one()) before waiting for the mutex (cond.wait(lock)), then while waiting, we do not get it. We lose a notification. Therefore, we must protect the notification too.

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #6204: If we send a notify(cond.notify_one()) before waiting for the mutex (cond.wait(lock)), then while waiting, we do not get it. We lose a notification. Therefore, we must protect the notification too.
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-12-11 23:27:55


#6204: If we send a notify(cond.notify_one()) before waiting for the mutex
(cond.wait(lock)), then while waiting, we do not get it. We lose a
notification. Therefore, we must protect the notification too.
-------------------------------+--------------------------------------------
  Reporter: kikots@… | Owner: anthonyw
      Type: Bugs | Status: new
 Milestone: To Be Determined | Component: thread
   Version: Boost 1.48.0 | Severity: Problem
Resolution: | Keywords: condition
-------------------------------+--------------------------------------------

Comment (by kikots@…):

 Replying to [comment:4 viboes]:
> Replying to [comment:2 kikots@…]:
> > 2.
 http://www.boost.org/doc/libs/1_48_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref
> > We can lose a notification.
> > {{{
> > boost::condition_variable cond;
> > boost::mutex mut;
> > bool data_ready;
> >
> > void process_data();
> >
> > void wait_for_data_to_process()
> > {
> > boost::unique_lock<boost::mutex> lock(mut);
> > while(!data_ready)
> > {
> > cond.wait(lock); // Only when we wating and has been unlocked
 mutex
> > // by the main thread, we can get
 notification.
> > // ... some code
> > // If in this moment one of thread sending notification, we
 lose it.
> > // For that would avoid this, we must lock all code
 (cond.notify_one())
> > // in all threads.
> > }
> > process_data();
> > }
> > }}}
>
> Sorry but I don't agree. Again condition variables are not semaphores.
 The behavior is the expected for me. Exit from a wait occurs when another
 thread notifies this condition and only then. Older notifications, that
 occurred before the call to wait, are naturally lost.

 Yes. And new notification after waiting we lose too, until not stop on
 waiting again.

 [http://www.boost.org/doc/libs/1_48_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref]
 {{{
 void prepare_data_for_processing()
 {
     retrieve_data();
     prepare_data();
     {
         boost::lock_guard<boost::mutex> lock(mut);
         data_ready=true;
     }
     cond.notify_one();
 }
 }}}

 It's normal for one thread that sends the notification, but not for two
 and more.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/6204#comment:5>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:08 UTC