Boost logo

Boost Users :

From: PJ Durai (pjdtech2000_at_[hidden])
Date: 2007-04-19 16:42:12


On 4/19/07, Peter Dimov <pdimov_at_[hidden]> wrote:
> PJ Durai wrote:
> > On 4/18/07, Ken <krovchuck_at_[hidden]> wrote:
> >>
> >> On Apr 13, 2007, PJ Durai wrote:
> >>>
> >>
> >>> I have two threads waiting on a condition. When I call
> >>> condition::notify_all() from the main thread, I expected both
> >>> threads to fall through and proceed simultaneously.
> >>>
> >>> But that is not what I am witnessing.
> >>> They behave like I had called condition::notify_one() twice.
> >>>
> >>> One thread continues, does its thing (in this case a simple Sleep
> >>> and a printf) comes back to wait. Only after that point the second
> >>> thread continued from the condition.
> >>>
> >>> Is this expected ? What am I missing ?
>
> This is expected. condition::wait reacquires the mutex after it returns. You
> then call Sleep(4000) with the mutex locked; the other threads can't
> continue because they are waiting for the mutex. Try unlocking lk before
> Sleep'ing.
>

Thanks peter.
That fixed it.
My loop looks like this now. And it is working as expected.

   void operator() ()
   {
      while(1)
      {
         {
            TRACE("%s : waiting ...\n", name.c_str());
            {
               TRACE("%s : before cond wait...\n", name.c_str());
               {
                  boost::mutex::scoped_lock lk(mutex);
                  cond.wait(lk);
               }
               TRACE("%s : after cond wait...\n", name.c_str());
               {
                  TRACE("%s : doing stuff...\n", name.c_str());

                  //ElapsedTime etime(name.c_str());
                  Sleep(4000);
                  for (int i=0; i<10; ++i)
                     std::cout << id;
                  std::cout << std::endl;
               }
            }
         }
         //barrier.wait();
      }
   }

Thanks Guys.


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