Class TaskCondition
Event loop based condition variable or "event" implementation.
class TaskCondition
: core .sync .condition .Condition
;
This class can be used in exchange for a core
to avoid blocking the event loop when waiting.
Constructors
Name | Description |
---|---|
this
(mtx)
|
|
this
(mtx)
|
Properties
Name | Type | Description |
---|---|---|
mutex [get]
|
core | |
mutex [get]
|
shared(core |
Methods
Name | Description |
---|---|
notify
()
|
|
notify
()
|
|
notifyAll
()
|
|
notifyAll
()
|
|
wait
()
|
|
wait
(timeout)
|
|
wait
()
|
|
wait
(timeout)
|
Notice
Because this class is annotated nothrow, it cannot be interrupted
using Task
. The corresponding
InterruptException
will be deferred until the next blocking
operation yields to the event loop.
Use InterruptibleTaskCondition
as an alternative that can be
interrupted.
Note that it is generally not safe to use a TaskCondition
together with an
interruptible mutex type.
See Also
Example
This example shows the typical usage pattern using a while
loop to make
sure that the final condition is reached.
import vibe .core .core;
import vibe .core .log;
__gshared Mutex mutex;
__gshared TaskCondition condition;
__gshared int workers_still_running = 0;
// setup the task condition
mutex = new Mutex;
condition = new TaskCondition(mutex);
logDebug("SETTING UP TASKS");
// start up the workers and count how many are running
foreach (i; 0 .. 4) {
workers_still_running++;
runWorkerTask(() nothrow {
// simulate some work
try sleep(100 .msecs);
catch (Exception e) {}
// notify the waiter that we're finished
{
auto l = scopedMutexLock(mutex);
workers_still_running--;
logDebug("DECREMENT %s", workers_still_running);
}
logDebug("NOTIFY");
condition .notify();
});
}
logDebug("STARTING WAIT LOOP");
// wait until all tasks have decremented the counter back to zero
synchronized (mutex) {
while (workers_still_running > 0) {
logDebug("STILL running %s", workers_still_running);
condition .wait();
}
}