4.23. Thread::Condition Class

Note: This class is not available with the PO_NO_THREAD_CLASSES parse option.

Condition objects, when used along with an AbstractSmartLock object (such as RWLock and Mutex objects), allow Qore threads to sleep until a certain condition becomes true.

Table 4.732. Condition Method Overview

Method

Except?

Description

Condition::constructor()

N

Creates the Condition object.

Condition::destructor()

N

Destroys the Condition object.

Condition::copy()

N

Creates a new Condition object, not based on the original.

nothing Condition::signal()

Y

Signals a single blocked thread to wake up.

nothing Condition::broadcast()

Y

Signals all threads blocked on this Condition object to wake up.

int Condition::wait(AbstractSmartLock $lock, timeout $timeout_ms = 0)

Y

Blocks a thread until signaled; accepts an optional timeout value.

int Condition::wait_count(AbstractSmartLock $lock)

N

Returns the number of threads currently blocked on this object.


4.23.1. Condition::constructor()

Synopsis

Creates the Condition object.

Prototype

Condition::constructor()

Example
my Condition $cond();

4.23.2. Condition::destructor()

Synopsis

Destroys the object.

Example
delete $cond;

4.23.3. Condition::copy()

Synopsis

Creates a new Condition object, not based on the original.

Example
my Condition $new_cond = $cond.copy();

4.23.4. Condition::signal()

Synopsis

Signals a single blocked thread to wake up. Normally this method call will be made while the same AbstractSmartLock object used for Condition::wait() calls is locked. Then, when the thread calling this method unlocks the AbstractSmartLock object, the thread(s) woken up by this call can continue executing.

Prototype

nothing Condition::signal()

Example
$cond.signal();

Table 4.733. Exceptions Thrown by Condition::signal()

err

desc

CONDITION-SIGNAL-ERROR

This exception should never be thrown - it indicates a low-level error in executing the method.


4.23.5. Condition::broadcast()

Synopsis

Wakes up all threads waiting on the Condition object. Normally this method call will be made while the same AbstractSmartLock object used for Condition::wait() calls is locked. Then, when the thread calling this method unlocks the AbstractSmartLock object, the thread(s) woken up by this call can continue executing.

Prototype

nothing Condition::broadcast()

Example
$cond.broadcast();

Table 4.734. Exceptions Thrown by Condition::broadcast()

err

desc

CONDITION-BROADCAST-ERROR

This exception should never be thrown - it indicates a low-level error in executing the method.


4.23.6. Condition::wait()

Synopsis

Blocks a thread on the Condition object. Must be called with an AbstractSmartLock argument, and the AbstractSmartLock must be locked before the call. This method will atomically unlock the AbstractSmartLock object and wait on this Condition object to be woken up with a Condition::signal() or Condition::broadcast() method call in another thread. At this point, the AbstractSmartLock will be reacquired with the same state as it was acquired previously before control returns to the blocked thread. The wait condition should always be tested again when the thread is unblocked.

Also accepts an optional timeout value in milliseconds. Like all Qore functions and methods taking timeout values, a relative time value may be passed instead of an integer to make the timeout units clear (ex: 2500ms for 2.5 seconds). Also if the call times out, the AbstractSmartLock will also be acquired when the Condition::wait() call returns and ETIMEDOUT will be returned.

Prototype

int Condition::wait(AbstractSmartLock $lock, timeout $timeout_ms = 0)

Example
{
    $mutex.lock();
    on_exit $mutex.unlock();
    while ($num > 0)
        $cond.wait($mutex);
    # ... do something
}

Table 4.735. Arguments for Condition::wait()

Argument

Description

AbstractSmartLock $lock

The AbstractSmartLock object to use for synchronization on this Condition object. The AbstractSmartLock must be locked before calling this method.

timeout $timeout_ms = 0

An optional timeout value; integers are interpreted as milliseconds; relative date/time values are interpreted literally.


Table 4.736. Return Values for Condition::wait()

Return Type

Description

int

0 for success, ETIMEDOUT if a timeout has occurred.


Table 4.737. Exceptions Thrown by Condition::wait()

err

desc

CONDITION-WAIT-ERROR

This exception should never be thrown - it indicates a low-level error in executing the method.


4.23.7. Condition::wait_count()

Synopsis

Returns the number of threads currently blocked on this object using the AbstractSmartLock passed; the AbstractSmartLock can be in any state (locked or unlocked) for this call (does not necessarily have to be locked).

Prototype

int Condition::wait_count(AbstractSmartLock $lock)

Example
$num_threads = $cond.wait_count();

Table 4.738. Arguments for Condition::wait()

Argument

Description

AbstractSmartLock $lock

The AbstractSmartLock to check for waiting threads. The AbstractSmartLock can be in any state (locked or unlocked) for this call (does not necessarily have to be locked).


Table 4.739. Return Values for Condition::wait_count()

Return Type

Description

int

The number of threads currently blocked on this object.