TaskCondition

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.

Constructors

this
this(core.sync.mutex.Mutex mtx)
Undocumented in source.

Members

Functions

notify
void notify()
Undocumented in source. Be warned that the author may not have intended to support it.
notifyAll
void notifyAll()
Undocumented in source. Be warned that the author may not have intended to support it.
wait
void wait()
Undocumented in source. Be warned that the author may not have intended to support it.
wait
bool wait(Duration timeout)
Undocumented in source. Be warned that the author may not have intended to support it.

Properties

mutex
Mutex mutex [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.

Examples

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();
	}
}

See Also

InterruptibleTaskCondition

Meta