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 |
|---|---|---|
N | Creates the Condition object. | |
N | Destroys the Condition object. | |
N | Creates a new Condition object, not based on the original. | |
Y | Signals a single blocked thread to wake up. | |
Y | Signals all threads blocked on this Condition object to wake up. | |
|
| Y | Blocks a thread until signaled; accepts an optional timeout value. |
N | Returns the number of threads currently blocked on this object. |
Creates the Condition object.
my Condition $cond();
Creates a new Condition object, not based on the original.
my Condition $new_cond = $cond.copy();
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.
$cond.signal();
Table 4.733. Exceptions Thrown by Condition::signal()
err | desc |
|---|---|
| This exception should never be thrown - it indicates a low-level error in executing the method. |
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.
$cond.broadcast();
Table 4.734. Exceptions Thrown by Condition::broadcast()
err | desc |
|---|---|
| This exception should never be thrown - it indicates a low-level error in executing the method. |
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.
int Condition::wait(AbstractSmartLock $lock, timeout $timeout_ms = 0)
{
$mutex.lock();
on_exit $mutex.unlock();
while ($num > 0)
$cond.wait($mutex);
# ... do something
}
Table 4.735. Arguments for Condition::wait()
Argument | Description |
|---|---|
| The AbstractSmartLock object to use for synchronization on this Condition object. The AbstractSmartLock must be locked before calling this method. |
| 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 |
|---|---|
0 for success, |
Table 4.737. Exceptions Thrown by Condition::wait()
err | desc |
|---|---|
| This exception should never be thrown - it indicates a low-level error in executing the method. |
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).
$num_threads = $cond.wait_count();
Table 4.738. Arguments for Condition::wait()
Argument | Description |
|---|---|
| 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 |
|---|---|
The number of threads currently blocked on this object. |