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.885. 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. | |
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.
new Counter([start_value]
)
$counter = new Counter();
Table 4.886. Arguments for Counter::constructor()
Argument | Type | Description |
---|---|---|
| Integer | If an argument is supplied here, then the Counter will be initialized with this value, otherwise the Counter is initialized with 0. |
Table 4.887. Return Values for Counter::constructor()
Return Type | Description |
---|---|
Counter Object | The new object created. |
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 lvalue
delete $counter;
Table 4.888. 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.
Counter::copy()
$new_counter = $counter.copy();
Table 4.889. Arguments for Counter::copy()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.890. Return Values for Counter::copy()
Return Type | Description |
---|---|
Counter Object | A new Counter object, not based on the original. |
Atomically increments the counter value.
Counter::inc()
$counter.inc();
Table 4.891. Arguments for Counter::inc()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
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()
$counter.dec();
Table 4.893. Arguments for Counter::dec()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.895. 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).
Counter::waitForZero([timeout_ms]
)
$counter.waitForZero();
Table 4.896. Arguments for Counter::waitForZero()
Argument | Type | Description |
---|---|---|
| Integer or Relative Date/Time | A value giving the number of milliseconds to wait for the Counter to reach zero. |
Table 4.897. Return Values for Counter::waitForZero()
Return Type | Description |
---|---|
Integer | If a timeout value was passed, -1 means that the call timed out, 0 means that the counter reached zero. |
Table 4.898. Exceptions Thrown by Counter::dec()
err | desc |
---|---|
| Object deleted in another thread. |
Returns the current counter value.
Counter::getCount()
$count = $counter.getCount();
Table 4.899. Arguments for Counter::getCount()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.900. Return Values for Counter::getCount()
Return Type | Description |
---|---|
Integer | The current counter value. |
Returns the number of threads currently blocked on this object.
Counter::getWaiting()
$threads = $counter.getWaiting();
Table 4.901. Arguments for Counter::getWaiting()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.902. Return Values for Counter::getWaiting()
Return Type | Description |
---|---|
Integer | The number of threads currently blocked on this object. |