Thread overview
Does anyone have an example of use of core.sync.Condition
Mar 30, 2014
Matt
Mar 30, 2014
Ali Çehreli
Mar 30, 2014
Jack Applegame
March 30, 2014
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.
March 30, 2014
On 03/30/2014 09:09 AM, 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.

I haven't used it either but I think this page explains the need for it:


http://en.wikipedia.org/wiki/Monitor_%28synchronization%29#Condition_variables

Ali

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


March 31, 2014
On Sun, 30 Mar 2014 12:09:35 -0400, Matt <webwraith@fastmail.fm> 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.

Google thread condition variable. It's a very well-known pattern.

-Steve