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-04 11:24:25


#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:
-------------------------------+--------------------------------------------

Comment (by kikots@…):

 I do not know it is the wrong source code or the wrong documentation. But
 if the documentation is correct, the error in the source code. And if the
 C++ code is correct, the documentation
 http://www.boost.org/doc/libs/1_48_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref
 should look like this:

 {{{
 void retrieve_data();
 void prepare_data();

 void prepare_data_for_processing()
 {
     retrieve_data();
     prepare_data();
     boost::lock_guard<boost::mutex> lock(mut);
     data_ready=true;
     cond.notify_one();
 }
 }}}

 We must unlock the mutex after sending the notification
 (cond.notify_one()), otherwise we may lose it. In general, the
 documentation does not meet the proper use of condition variable.


 The simplest examples are:
 1. http://liveworkspace.org/code/88059b47c4476d67c6abc44e31a90ade
 We lose a notification.
 {{{
 #include <iostream>
 #include <boost/thread/thread.hpp>
 #include <boost/thread/mutex.hpp>
 #include <boost/thread/locks.hpp>

 boost::condition_variable query_done_cond;
 boost::mutex mut;

 int main()
 {
   boost::unique_lock<boost::mutex> lock0(mut);

   query_done_cond.notify_one();
   std::cout << "wait t0 \n";
   query_done_cond.wait(lock0);
   std::cout << "get t0 \n";

   return 0;
 }
 }}}
 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();
 }
 }}}
 3. http://liveworkspace.org/code/db3576e5bf5a5c128152762ba0f30027
 We are sending the notification (query_done_cond.notify_one()) in second
 thread (t1) between first and second the waiting
 (query_done_cond.wait(lock0)) in main thread. And we has lost this
 notification.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/6204#comment:2>
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:07 UTC