bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Question regarding the Conditional class


From: DWyc
Subject: Question regarding the Conditional class
Date: Mon, 5 Nov 2001 19:04:32 -0800 (PST)

I'm experiencing a situation where a thread's call to
Conditional::Wait() will -not- be unblocked when
another thread calls Conditional::Signal() for the
same Conditional instance.  I began to wonder if I was
doing something wrong with the Conditional, so I
experimented.  Here's a very contrived test I'd like
to ask you about:

/***Example.cpp ***/
class NextTest : public Thread
{
public:
  NextTest(Conditional& Locker,int WichMethod) :
    m_Locker(Locker) {Start();}
  void ThisWorks(void) {
    m_Locker.EnterMutex();
    m_Locker.Wait();
    m_Locker.LeaveMutex();
  }
  void ThisDoesnt(void)   {  m_Locker.Wait();  }
  void Run(void)  {
    for(;;) {
      if( m_WhichMethod )  ThisWorks();
      else                 ThisDoesnt();
      puts("No longer locked");
    }
  }
private:
  Conditional& m_Locker;
};

int main(int argc,char *argv[])
{
  Conditional cond;
  NextTest    next( cond,atoi(argv[1]) );

  for(;;)
  {
    sleep(1);
    puts("About to wake up thread");
    cond.Signal(false);
    puts("Finished waking thread");
  }
}
/*** end example ***/

I expect the main process to wake up the thread once
per second;  I expect the thread to anounce the
Signal() when it exits the Wait(), and then return to
a Wait() until again Signal()'ed, so on and on.

A call to method ThisDoesnt() never blocks on the call
to Wait() and we get a rapid stream of output -- not
output on a 1-second interval.  But a call to
ThisWorks() does what I expect, and the reporting is
paced exactly as anticipated.

Question:  Why won't ThisDoesnt()'s call to Wait()
block?

Question:  Why is it, in ThisWorks(), that if I
EnterMutex() then Wait(), then ExitMutex(), I get the
functionality I'm looking for?  Somehow it seems
strange that I have to work these two extra steps, and
that makes me wonder if I am using them incorrectly.

Question:  In a larger program I wrote, based on the
structure of ThisWorks() for the conditional blocking,
when the thread is in a Wait() call, another thread
calls the Conditional::Signal() to unblock the thread,
but the thread never unblocks.  Oddly enough, my
example (ThisWorks()) functions as I anticipated.  So
I suspect I'm not using Conditional correctly.  Is
there something I'm doing wrong that might cause the
erronious behavior I've mentioned?


Many thanks!


Sincerely,

D Wyc
 


__________________________________________________
Do You Yahoo!?
Find a job, post your resume.
http://careers.yahoo.com



reply via email to

[Prev in Thread] Current Thread [Next in Thread]