Thread overview | ||||||
---|---|---|---|---|---|---|
|
March 30, 2014 Does anyone have an example of use of core.sync.Condition | ||||
---|---|---|---|---|
| ||||
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 Re: Does anyone have an example of use of core.sync.Condition | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt | 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 Re: Does anyone have an example of use of core.sync.Condition | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt | 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 Re: Does anyone have an example of use of core.sync.Condition | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt | 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
|
Copyright © 1999-2021 by the D Language Foundation