#include <CCondition.h> structCConditionAttributes
{void setShared();
void setUnshared();
bool isShared();
classCConditionVariable
{CConditionVariable();
CConditionVariable(pthread_condattr_t& attr);
CConditionVariable(CConditionAttributes& attr);
void signal();
void broadcast();
bool timedwait(CMutex& mutex, const struct timespec* abstime);
bool timedwait(pthread_mutex_t* mutex, const struct timespec* abstime);
bool timedwait(CMutex& mutex, int milliseconds);
bool timedwait(pthread_mutex_t* mutex, int milliseconds) throws ;
void wait(CMutex& mutex);
void wait(pthread_mutex_t* mutex) throws ;
};
This class encapsulates POSIX pthread condition variables. A condition variable allows a thread to atomically wait for another thread to signal a condition. Condition variables are used in conjunction with mutexes as follows (once the mutex and condition variable are created):
Thread 1 takes the mutex.
Thread 1 performs one of the wait operations. The condition variable atomically releases the mutex and blocks the thread on the condition varaible.
Thread 2 locks the same mutext (associated with the condition variable).
Thread 2 signals to wake the next thread blocked on the condition or broadcasts to wake all blocked threads. At this time, Thread 1's condition variable block is completed and the operating system attempts to acquire the mutex which keeps thread 1 blocked.
Thread 2 unlocks the mutex. At this time, Thread 1 gains the mutex lock and becomes unblocked.
Thread 2 releases its mutex lock.
This class is intended to be used either with raw pthread mutexes
pthread_mutex_t* and
The CMutex
class wrapper for pthread mutexes.
CConditionVariable();
Constructs a condition variable with the default attrributes.
CConditionVariable(pthread_condattr_t& attr);
Constructs a condition variable with attributes defined by
the raw pthreads condition variable attr
as its attributes.
CConditionVariable(CConditionAttributes& attr);
Constructs a condition variable with attributes set by
the CConditionAttrbiutes
attr
parameter.
void signal();
Signals the condition variable. The calling thread must have a lock on the mutex associated with the waits on the condition variable. One thread will be released from condition wait (however it will still be waiting to obtain the mutext lock).
void broadcast();
Broadcasts a condition variable. This is the same as
signal
except all waiting
threads are released from condition wait and attempt
to acquire the associated mutex.
bool timedwait(CMutex& mutex, const struct timespec* abstime);
Waits on the condition variable until either the condition
variable is signalled (or broadcast), or until the absolute
time specified by abstime
is
reached. The thread must be holding the mutex
to call this.
If the condition variable was signalled before the timeout,
true is returned and the thread is
holding the mutex
.
If the wait timed out, the method returns
false and the thread is not holding
the mutex.
bool timedwait(pthread_mutex_t* mutex, const struct timespec* abstime);
This is the same as the previous function however
the mutex
parameter is a raw
posix mutex rather than one wrapped in a
CMutex
object.
bool timedwait(CMutex& mutex, int milliseconds);
bool timedwait(pthread_mutex_t* mutex, int milliseconds)
throws ;
Same as above, however the timeout is specified in milliseconds from the time of the call.
void wait(CMutex& mutex);
void wait(pthread_mutex_t* mutex)
throws ;
Waits for the mutex without any bound on the timet the thread will block.
The behavior of pthread entities is controled by an attribute
block that is passed to the methods that create them.
CConditionAttributes
is a struct that
wraps the pthread_condattr_t attribute block for
condition variables with methods that make it easy to set up the
block.
void setShared();
Sets the sharing attribute to: PTHREAD_PROCESS_SHARED.
void setUnshared();
Sets the sharing attribute to: PTHREAD_PROCESS_PRIVATE
bool isShared();
Returns true if the sharing attribute is PTHREAD_PROCESS_SHARED