[ECOS] CONDITION VARIABLES & MUTEXES

Srinivasan Sriram pet_jimmy@yahoo.com
Wed Jun 20 00:27:00 GMT 2001


Hello!

I'm trying out condition variables for 
the first time.
As per the reference manual,
cyg_cond_wait releases the mutex (I don't 
have to explicitly use 
cyg_mutex_release(&mut)).

But incase of cyg_cond_broadcast, I quote 
from the documentation - "Wakes all the 
threads waiting on the condition variable.
 Each time a thread is awakened it will
become the current owner of the mutex."

But the threads waiting on the condition 
variables are not awakened until I 
explicitly release the mutex after 
cyg_cond_broadcast call.

Please look at my dummy code where thr1 
and thr3 act as consumers and thr2 is the 
producer.

-----
#include<stdio.h>
#include<cyg/kernel/kapi.h>
 
cyg_thread thr[3];
char stack[3][4096];
cyg_handle_t hthr1,hthr2,hthr3;
cyg_thread_entry_t fthr1,fthr2,fthr3;
cyg_thread thr1,thr2,thr3;
cyg_mutex_t mut;
cyg_cond_t cond;
int i;
 
cyg_user_start()
{
 
cyg_thread_create(1,fthr1,(cyg_addrword_t)0,"THREADA",(void
*)stack[0],4096,&hthr1,&thr[0]);

cyg_thread_create(2,fthr2,(cyg_addrword_t)0,"THREADB",(void
*)stack[1],4096,&hthr2,&thr[1]);

cyg_thread_create(1,fthr3,(cyg_addrword_t)0,"THREADC",(void
*)stack[2],4096,&hthr3,&thr[2]);
cyg_thread_resume(hthr1);
cyg_thread_resume(hthr2);
cyg_thread_resume(hthr3);
cyg_mutex_init(&mut);
cyg_cond_init(&cond,&mut);
}
 
 
void  fthr1(cyg_addrword_t y)
{
 
cyg_bool_t a;
a=cyg_mutex_lock(&mut);
printf("thr1 %d\n",(char *)a);
 
cyg_cond_wait(&cond);
printf("thr1..unlocking \n");
cyg_mutex_unlock(&mut);
 
printf("thr1 exiting \n");
}
 
void fthr2(cyg_addrword_t y)
{
cyg_bool_t a;
 
a=cyg_mutex_lock(&mut);
printf("thr2 %d\n",a);
 
cyg_thread_delay(200);
cyg_cond_broadcast(&cond);
printf("thr2..after broadcast \n");

cyg_mutex_unlock(&mut); /*I HAVE TO EXPLICITLY USE
THIS*/

printf("thr2 exiting....\n");
}
 
void fthr3(cyg_addrword_t y)
{
cyg_bool_t a1;
a1=cyg_mutex_lock(&mut);
printf("thr3 %d\n",a1);
 
cyg_cond_wait(&cond);
printf("thr3...unlocking \n");
 
cyg_mutex_unlock(&mut);
printf("thr3 exiting \n ");
}
                                      
----------
I GET THE FOLLOWING O/P ->

thr1 1
thr3 1
thr2 1
thr2..after broadcast
thr1..unlocking
thr1 exiting
thr3.unlocking
thr3 exiting
thr2 exiting....

----
IN CASE I COMMENT CYG_MUTEX_UNLOCK AND EXPECT
CYG_COND_BROADCAST  TO AUTOMATICALLY RELEASE THE 
MUTEX, THE THREADS THR1 AND THR3 ARE NOT AWAKENED. THE
O/P IS -

thr1 1
thr3 1
thr2 1
thr2..after broadcast
thr2 exiting....   

--------------------

1.
Can anyone explain this? Why should thr2 release the
mutex when cyg_cond_broadcast should automatically
release it?                                 

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?

ALSO COULD ANYONE EXPLAIN - WHAT IS THE VARIABLE IN
THE WHILE LOOP. WHAT DOES IT TEST?

thanks
Kavita


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/



More information about the Ecos-discuss mailing list