www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does anyone have an example of use of core.sync.Condition

reply "Matt" <webwraith fastmail.fm> writes:
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.
Mar 30 2014
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Mar 30 2014
prev sibling next sibling parent "Jack Applegame" <japplegame gmail.com> writes:
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(); }
Mar 30 2014
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Mar 31 2014