[ECOS] Re: CONDITION VARIABLES & MUTEXES

Jonathan Larmour jlarmour@redhat.com
Wed Jun 20 11:02:00 GMT 2001


Srinivasan Sriram wrote:
> 
> 1.
> Can anyone explain this? Why should thr2 release the
> mutex when cyg_cond_broadcast should automatically
> release it?

You read too much into the documentation. When it says that
cyg_cond_broadcast() wakes all threads waiting on the CVS and the wakened
thread becomes the owner of the mutex, it *doesn't* mean that the broadcast
does an implicit unlock. Producer threads are not meant to be locking and
unlocking the mutex underlying the cv.
 
> 2. The ecos documentation says, I quote -
> The thread can only be awakened by a call to
> cyg_cond_signal() or
> cyg_cond_broadcast() on the same condition variable.
> When the thread is awakened, the
> mutex will be reclaimed before this function proceeds.
> Since it may have to wait for this,
> cyg_cond_wait() should only be used in a loop since
> the condition may become false in the
> meantime. This is shown in the following example:
> 
>   extern cyg_mutex_t mutex;
>   extern cyg_cond_t cond;
> 
>   cyg_mutex_lock( &mutex );
>   ...
> 
>   while( condition_not_true )
>   {
>    cyg_cond_wait( &cond );
>   }
> 
>   ...
> 
>   cyg_mutex_unlock( &mutex );
> 
> ------
> 
> WHY DO WE REQUIRE THE WHILE LOOP? ANY WAY THE
> CYG_COND_WAIT CANNOT PROCEED BEFORE IT OBTAINS THE
> MUTEX AND BROADCAST FROM THE PRODUCER AND OTHER
> THREADS?

Two threads waiting on the condition may be woken up. They won't run at the
same time since only one can have the mutex locked, but when the second one
runs, the condition will not be true any more.
 
> ALSO COULD ANYONE EXPLAIN - WHAT IS THE VARIABLE IN
> THE WHILE LOOP. WHAT DOES IT TEST?

The condition you are waiting for...

As Robin says, you should read some general books about concurrency theory.
There's probably stuff on the web, or I can recommend Concurrent Systems by
J. Bacon published by Addison Wesley.

Jifl
-- 
Red Hat, Rustat House, Clifton Road, Cambridge, UK. Tel: +44 (1223) 271062
Maybe this world is another planet's Hell -Aldous Huxley || Opinions==mine
Come to the Red Hat TechWorld open source conference in Brussels!
Keynotes, techie talks and exhibitions    http://www.redhat-techworld.com/



More information about the Ecos-discuss mailing list