vibe.d beta banner
get vibe.d
0.10.1

Asynchronous I/O that doesn’t get in your way, written in D

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.sync.condition.Condition to avoid blocking the event loop when waiting.

Constructors

NameDescription
this (mtx)

Properties

NameTypeDescription
mutex[get] core.sync.mutex.Mutex

Methods

NameDescription
notify ()
notifyAll ()
wait ()
wait (timeout)

Notice

Because this class is annotated nothrow, it cannot be interrupted using 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.

See Also

InterruptibleTaskCondition

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({
		// simulate some work
		sleep(100.msecs);

		// notify the waiter that we're finished
		synchronized (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();
	}
}
Authors

Leonid Kramer, Sönke Ludwig, Manuel Frischknecht

Copyright

© 2012-2019 Sönke Ludwig

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.