bug-commoncpp
[Top][All Lists]
Advanced

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

Signalling a condition


From: Albert Strasheim
Subject: Signalling a condition
Date: Wed, 1 Jan 2003 23:08:25 +0200
User-agent: Mutt/1.4i

Hello,

I have a question about Conditional::signal().

According to the glibc manual (describing pthread_cond_wait),

"Unlocking the mutex and suspending on the condition variable is done 
atomically. Thus, if all threads always acquire the mutex before 
signaling the condition, this guarantees that the condition cannot be 
signaled (and thus ignored) between the time a thread locks the mutex 
and the time it waits on the condition variable."

The current code:

void ost::Conditional::signal(bool broadcast) 
{
  if(broadcast)
    pthread_cond_broadcast(&_cond);
  else
    pthread_cond_signal(&_cond);
}

But according to the glibc manual, "... if all threads always acquire 
the mutex before signaling the condition ...".

The new signal method would then read:

void ost::Conditional::signal(bool broadcast) 
{
  enterMutex();
  if(broadcast)
    pthread_cond_broadcast(&_cond);
  else
    pthread_cond_signal(&_cond);
  leaveMutex();
}

What will happen now is that all the threads that called
pthread_cond_wait will start waking up, and one will acquire the mutex. 
It will then possibly do something, and then unlock the mutex and go 
about its business, allowing the next thread to complete its
pthread_cond_wait call and so forth.

Comments? Am I even correct? :-)

Cheers,

Albert



reply via email to

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