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(); } }
InterruptibleTaskCondition
Event loop based condition variable or "event" implementation.
This class can be used in exchange for a core.sync.condition.Condition to avoid blocking the event loop when waiting.
Notice: Because this class is annotated nothrow, it cannot be interrupted using vibe.core.task.Task.interrupt(). 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.