Using of core.sync.condition.Condition
Alexander
aldem+dmars at nk7.net
Fri Oct 21 11:32:15 PDT 2011
Hi,
I've the code (see below) which produces an exception (SyncException "Unable to wait for condition")
unless "synchronized" is used when waiting on condition (Fedora Linux, 32 bit, DMD 2.055).
Do I do something wrong? Using "synchronized" when accessing anything that is synchronization object
by itself is a little bit counter-intuitive, IMHO.
---snip---
import std.conv;
import std.stdio;
import core.thread;
import core.sync.mutex;
import core.sync.condition;
__gshared Mutex mutex;
__gshared Condition cond;
void logs(string text)
{
synchronized {
writeln(text);
}
}
void worker()
{
logs("Worker started");
while (true) {
//synchronized (mutex)
{
try {
cond.wait();
} catch (Exception ex) {
logs("Oops: %s" ~ to!string(ex));
return;
}
}
logs("Got notify");
}
}
void main()
{
mutex = new Mutex();
cond = new Condition(mutex);
(new Thread(&worker)).start();
(new Thread(&worker)).start();
(new Thread(&worker)).start();
(new Thread(&worker)).start();
Thread.sleep(dur!("msecs")(250));
logs("Sending notify");
cond.notifyAll();
thread_joinAll();
}
---snip---
--
/Alexander
More information about the Digitalmars-d
mailing list