Qore Programming Language Reference Manual: Qore::Thread::Counter Class Reference (original) (raw)
Implements a class that can be used for blocking a thread until a counter reaches zero. More...
#include <[QC_Counter.dox.h](%5Fq%5Fc%5F%5F%5Fcounter%5F8dox%5F8h%5Fsource.html)>
Public Member Methods | |
---|---|
constructor (softint c=0) | |
Creates the Counter object. More... | |
copy () | |
Creates a new Counter object with the same count as the original. More... | |
int | dec () |
Atomically decrements the counter value. More... | |
destructor () | |
Destroys the Counter object. More... | |
int | getCount () |
Returns the current counter value. More... | |
int | getWaiting () |
Returns the number of threads currently blocked on this object. More... | |
int | inc () |
Atomically increments the counter value. More... | |
nothing | waitForZero () |
Blocks a thread until the counter reaches zero. More... | |
int | waitForZero (timeout timeout_ms) |
Blocks a thread until the counter reaches zero. More... | |
Implements a class that can be used for blocking a thread until a counter reaches zero.
Restrictions:
Counter objects allow Qore threads to sleep until a counter reaches zero.
Note
This class is not available with the PO_NO_THREAD_CLASSES parse option.
◆ constructor()
Qore::Thread::Counter::constructor | ( | softint | c = 0 | ) |
---|
Creates the Counter object.
Parameters
c | an argument is supplied here, then the Counter will be initialized with this value, otherwise the Counter is initialized with 0 |
---|
Example:
Exceptions
COUNTER-ERROR | a negative number was passed to initialize the Counter |
---|
◆ copy()
Qore::Thread::Counter::copy | ( | ) |
---|
Creates a new Counter object with the same count as the original.
Example:
Counter new_counter = counter.copy();
◆ dec()
int Qore::Thread::Counter::dec | ( | ) |
---|
Atomically decrements the counter value.
A COUNTER-ERROR
exception can 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 practice with correct code.
Example:
Returns
the current value after the decrement is returned
Exceptions
COUNTER-ERROR | Counter has been deleted in another thread or Counter is already at 0 |
---|
Since
Qore 0.8.13 this method returns the current value after the decrement
◆ destructor()
Qore::Thread::Counter::destructor | ( | ) |
---|
Destroys the Counter 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 also in each thread blocked on this object when it is deleted.
Example:
Exceptions
COUNTER-ERROR | Object deleted while other threads blocked on it |
---|
◆ getCount()
int Qore::Thread::Counter::getCount | ( | ) |
---|
Returns the current counter value.
Returns
the current counter value
Code Flags:
Example:
int c = counter.getCount();
◆ getWaiting()
int Qore::Thread::Counter::getWaiting | ( | ) |
---|
Returns the number of threads currently blocked on this object.
Returns
the number of threads currently blocked on this object
Code Flags:
Example:
int c = counter.getWaiting();
◆ inc()
int Qore::Thread::Counter::inc | ( | ) |
---|
Atomically increments the counter value.
Example:
Returns
the current value after the increment is returned
Since
Qore 0.9 this method returns the current value after the increment
◆ waitForZero() [1/2]
nothing Qore::Thread::Counter::waitForZero | ( | ) |
---|
Blocks a thread until the counter reaches zero.
Example:
Exceptions
COUNTER-ERROR | Counter has been deleted in another thread |
---|
◆ waitForZero() [2/2]
int Qore::Thread::Counter::waitForZero | ( | timeout | timeout_ms | ) |
---|
Blocks a thread until the counter reaches zero.
Parameters
Returns
0 on sucess, or non-zero if a timeout occurred
Example:
if (counter.waitForZero(1500))
throw "TIMEOUT", "counter did not reach 0 in 1.5s";
Exceptions
COUNTER-ERROR | Counter has been deleted in another thread |
---|