bug-commoncpp
[Top][All Lists]
Advanced

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

Re: Follow Up: Question regarding the Conditional class


From: David Sugar
Subject: Re: Follow Up: Question regarding the Conditional class
Date: Mon, 29 Sep 2003 12:09:26 -0400
User-agent: KMail/1.5

Remember that the functioning of a posix conditional depends on the associated 
mutex.  Based on that, I kind of see why it may be nessisary to approach the 
mutex at the same time the wait is occuring, and I may change wait to do this 
automatically.  However, this brings a question about having multiple threads 
waiting on a conditional.  This example also seems to be with Common C++, 
rather than with Common C++ "2".

On Monday 29 September 2003 11:14 am, zze-BARAULT Eric FTRD/DAC/LAN wrote:
> hi,
>
> i saw this post on the common c++ forum:
>
> ----------------------
>
> 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
>
> --------------------------
>
>
> I encounter the same problem and just can't see what is the trick to make
> it work...
>
> Could you please help??
>
>
> Regards,
>
> Eric Barault





reply via email to

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