Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

WaitCondition Class Reference

This class implements a condition (condition variable). More...

#include <condition.h>

Collaboration diagram for WaitCondition:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 WaitCondition ()
 Set up a condition,.

 ~WaitCondition ()
 Destroy a condition.

void signal ()
 If threads are waiting, exactly one of them is woken up.

void broadcast ()
 If threads are waiting, all of them are woken up.

void wait (ConditionMutex &m)
 Wait until this thread is woken up on the condition.

int wait (ConditionMutex &m, long msec)
 Like wait but with additional timeout.


Private Attributes

GCond * c

Detailed Description

This class implements a condition (condition variable).

Author:
Wolfgang Wieser ] wwieser (a) gmx <*> de [
A condition (short for "condition variable") is a synchronization device that allows threads to suspend execution and relinquish the processors until some predicate on shared data is satisfied. The basic operations on conditions are: signal the condition (when the predicate becomes true), and wait for the condition, suspending the thread execution until another thread signals the condition.

A condition variable must always be associated with a mutex, to avoid the race condition where a thread prepares to wait on a condition variable and another thread signals the condition just before the first thread actually waits on it.

Definition at line 57 of file condition.h.


Constructor & Destructor Documentation

WaitCondition::WaitCondition  ) 
 

Set up a condition,.

Definition at line 72 of file condition.cc.

References c.

WaitCondition::~WaitCondition  ) 
 

Destroy a condition.

Definition at line 85 of file condition.cc.

References c.


Member Function Documentation

void WaitCondition::broadcast  )  [inline]
 

If threads are waiting, all of them are woken up.

Restarts all the threads that are waiting on the condition variable Nothing happens if no threads are waiting.

See also:
signal()

Definition at line 98 of file condition.h.

References c, and USE_PRIVATE__PTHREADS.

Referenced by Semaphore::post().

void WaitCondition::signal  )  [inline]
 

If threads are waiting, exactly one of them is woken up.

Restarts one of the threads that are waiting on the condition If no threads are waiting on it, nothing happens. If several threads are waiting on the condition, exactly one is restarted, but it is not specified which one.

See also:
broadcast()

Definition at line 83 of file condition.h.

References c, and USE_PRIVATE__PTHREADS.

int WaitCondition::wait ConditionMutex m,
long  msec
[inline]
 

Like wait but with additional timeout.

Limit the waiting time to the passed number of milliseconds.

Returns:
  • 0 if woken up on time,
  • 1 if the timeout expired,
  • 2 if interrupted by a system call
See wait above for detailed information.

Definition at line 37 of file condition.cc.

References c, and ConditionMutex::m.

void WaitCondition::wait ConditionMutex m  )  [inline]
 

Wait until this thread is woken up on the condition.

The mutex is unlocked before falling asleep and locked again before resuming, so always enter with a locked mutex.

Note:
It is important to use the wait() functions only inside a loop, which checks for the condition to be true as it is not guaranteed that the waiting thread will find it fulfilled, even if the signaling thread left the condition in that state. This is because another thread can have altered the condition, before the waiting thread got the chance to be woken up, even if the condition itself is protected by a mutex.
Usage example: See Semaphore::post() and Semaphore::wait() in file semaphore.cc (the semaphore emulation implementation).

Note:
You must use a special ConditionMutex, not a "normal" FastMutex or RecursiveMutex.

Definition at line 126 of file condition.h.

References c, and USE_PRIVATE__PTHREADS.

Referenced by Semaphore::wait().


Member Data Documentation

GCond* WaitCondition::c [private]
 

Definition at line 63 of file condition.h.

Referenced by broadcast(), signal(), wait(), WaitCondition(), and ~WaitCondition().


The documentation for this class was generated from the following files:
Generated on Sat Feb 19 22:35:54 2005 for Ray by doxygen 1.3.5