Note: This class is not available with the PO_NO_THREAD_CLASSES parse option.
Counter objects allow Qore threads to sleep until a counter reaches zero.
Table 4.664. Counter Method Overview
Method | Except? | Description |
|---|---|---|
|
| N | Creates the Counter object. |
Y | Destroys the Counter object. | |
N | Creates a new Counter object, not based on the original. | |
|
| N | Atomically increments the counter value. |
|
| Y | Atomically decrements the counter value. |
|
| Y | Blocks a thread until the counter reaches zero; returns zero on sucess, or non-zero if a timeout occurred. |
|
| N | Returns the current counter value. |
|
| N | Returns the number of threads currently blocked on this object. |
Creates the Counter object; an optional integer argument may be given giving the intial value of the Counter. If no value is given, then the Counter is initialized with 0.
Counter::constructor(softint $count = 0)
my Counter $counter();
Table 4.665. Arguments for Counter::constructor()
Argument | Description |
|---|---|
| If an argument is supplied here, then the Counter will be initialized with this value, otherwise the Counter is initialized with 0. |
Destroys the object. Note that it is a programming error to delete this object while other threads are blocked on it; in this case an exception is thrown in the deleting thread, and in each thread blocked on this object when it is deleted.
delete $counter;
Table 4.666. Exceptions Thrown by Counter::destructor()
err | desc |
|---|---|
| Object deleted while other threads blocked on it. |
Creates a new Counter object, not based on the original.
my Counter $new_counter = $counter.copy();
Atomically increments the counter value.
Counter::inc() returns nothing
$counter.inc();
Atomically decrements the counter value. An exception can only be thrown if the object is deleted in another thread while this call is in progress; this is a race condition caused by a user programming error and should not occur in practise with correct code.
Counter::dec() returns nothing
$counter.dec();
Table 4.667. Exceptions Thrown by Counter::dec()
err | desc |
|---|---|
| Object deleted in another thread. |
Blocks a thread until the counter reaches zero. Takes an optional timeout value in milliseconds. Like all Qore functions and methods taking timeout values, a relative date/time value may be passed instead of an integer to make the timeout units clear (ex: 2500ms for 2.5 seconds).
Returns -1 if the call times out (only if a timeout value is passed), 0 if the counter reached zero.
Counter::waitForZero() returns nothing
Counter::waitForZero(softint $timeout_ms) returns int
Counter::waitForZero(date $timeout) returns int
$counter.waitForZero();
Table 4.668. Arguments for Counter::waitForZero()
Argument | Description |
|---|---|
An optional timeout value to wait for the Counter to reach zero; integers are interpreted as milliseconds; relative date/time values are interpreted literally. |
Table 4.669. Return Values for Counter::waitForZero()
Return Type | Description |
|---|---|
If a timeout value was passed, -1 means that the call timed out, 0 means that the counter reached zero. |
Table 4.670. Exceptions Thrown by Counter::dec()
err | desc |
|---|---|
| Object deleted in another thread. |
Returns the current counter value.
Counter::getCount() returns int
my int $count = $counter.getCount();
Returns the number of threads currently blocked on this object.
Counter::getWaiting() returns int
my int $threads = $counter.getWaiting();
Table 4.672. Return Values for Counter::getWaiting()
Return Type | Description |
|---|---|
The number of threads currently blocked on this object. |