Does anyone have an example of use of core.sync.Condition

Jack Applegame japplegame at gmail.com
Sun Mar 30 11:47:28 PDT 2014


On Sunday, 30 March 2014 at 16:09:37 UTC, Matt wrote:
> I have little experience in multi-threading programming, and 
> was digging into std.concurrency, but I don't really understand 
> the Condition class as it was used there. Could someone provide 
> a bare-bones use of this class? I would be much obliged, thanks.

Simple example of sending signal from one thread to another.

import std.stdio;
import core.thread;
import core.sync.condition;


class Foo {
	bool signal = false;
	Condition condition;
	this() {
		condition = new Condition(new Mutex);
	}
	void sendSignal() {
		writeln("sending signal");
		synchronized(condition.mutex) {
			signal = true;
			condition.notify();
		}
		writeln("signal sent");
	}
	void waitForSignal() {
		new Thread({
			writeln("waiting for signal");
			synchronized(condition.mutex) {
				while(!signal) {
					condition.wait();
				}
			}
			writeln("signal received");
		}).start();
	}
}

void main() {
	auto foo = new Foo;
	foo.waitForSignal();
	Thread.sleep(2.seconds);
	foo.sendSignal();
}




More information about the Digitalmars-d-learn mailing list