4.25. Thread::Counter Class

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

Counter::constructor(softint $count = 0)

N

Creates the Counter object.

Counter::destructor()

Y

Destroys the Counter object.

Counter::copy()

N

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

Counter::inc() returns nothing

N

Atomically increments the counter value.

Counter::dec() returns nothing

Y

Atomically decrements the counter value.

Counter::waitForZero() returns nothing

Counter::waitForZero(softint $timeout_ms) returns int

Counter::waitForZero(date $timeout) returns int

Y

Blocks a thread until the counter reaches zero; returns zero on sucess, or non-zero if a timeout occurred.

Counter::getCount() returns int

N

Returns the current counter value.

Counter::getWaiting() returns int

N

Returns the number of threads currently blocked on this object.


4.25.1. Counter::constructor()

Synopsis

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.

Prototype

Counter::constructor(softint $count = 0)

Example
my Counter $counter();

Table 4.665. Arguments for Counter::constructor()

Argument

Description

softint $count = 0

If an argument is supplied here, then the Counter will be initialized with this value, otherwise the Counter is initialized with 0.


4.25.2. Counter::destructor()

Synopsis

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.

Example
delete $counter;

Table 4.666. Exceptions Thrown by Counter::destructor()

err

desc

COUNTER-ERROR

Object deleted while other threads blocked on it.


4.25.3. Counter::copy()

Synopsis

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

Example
my Counter $new_counter = $counter.copy();

4.25.4. Counter::inc()

Synopsis

Atomically increments the counter value.

Prototype

Counter::inc() returns nothing

Example
$counter.inc();

4.25.5. Counter::dec()

Synopsis

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.

Prototype

Counter::dec() returns nothing

Example
$counter.dec();

Table 4.667. Exceptions Thrown by Counter::dec()

err

desc

COUNTER-ERROR

Object deleted in another thread.


4.25.6. Counter::waitForZero()

Synopsis

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.

Prototype

Counter::waitForZero() returns nothing

Counter::waitForZero(softint $timeout_ms) returns int

Counter::waitForZero(date $timeout) returns int

Example
$counter.waitForZero();

Table 4.668. Arguments for Counter::waitForZero()

Argument

Description

[softint $timeout_ms or date $timeout]

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

int

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

COUNTER-ERROR

Object deleted in another thread.


4.25.7. Counter::getCount()

Synopsis

Returns the current counter value.

Prototype

Counter::getCount() returns int

Example
my int $count = $counter.getCount();

Table 4.671. Return Values for Counter::getCount()

Return Type

Description

int

The current counter value.


4.25.8. Counter::getWaiting()

Synopsis

Returns the number of threads currently blocked on this object.

Prototype

Counter::getWaiting() returns int

Example
my int $threads = $counter.getWaiting();

Table 4.672. Return Values for Counter::getWaiting()

Return Type

Description

int

The number of threads currently blocked on this object.