Condition Mutexes
"Jérôme M. Berger"
jeberger at free.fr
Wed Oct 21 12:49:45 PDT 2009
dsimcha wrote:
> == Quote from Bartosz Milewski (bartosz-nospam at relisoft.com)'s article
>> dsimcha Wrote:
>>> void main() {
>>> condition = new Condition( new Mutex() );
>>> auto T = new Thread(&waitThenPrint);
>>> T.start();
>>> condition.notify(); // Never wakes up and prints FOO.
>>> }
>> Your program terminates immediately after sending the notification. You need to
> stall the exit until the other thread has a chance to wake up.
>
> Thanks. I've implemented this, along w/ one other suggestion from another poster.
> Here's the new program. It still doesn't work. Has anyone successfully used
> core.sync.condition from druntime *on D2, not the Tango version on D1*? If it
> works, then it should be better documented so people who aren't already threading
> gurus can figure out how to use it. If it doesn't work, then as soon as I can
> confirm that I'm not the problem, I'll go file a bug report.
>
> Bartosz, since you're a threading guru, could you please write a simple test
> program using core.sync.condition and see if it works, and if not either file a
> bug report or let me know?
>
> import core.sync.mutex, core.sync.condition, core.thread, std.stdio;
>
> __gshared Condition condition;
> __gshared Mutex mutex;
>
> void waitThenPrint() {
> mutex.lock;
> condition.wait();
> mutex.unlock;
> writeln("FOO");
> }
>
> void main() {
> mutex = new Mutex;
> condition = new Condition(mutex);
> auto T = new Thread(&waitThenPrint);
> T.start();
> condition.notify(); // Never wakes up and prints FOO.
> T.join;
> }
Well, you should lock the mutex before calling condition.notify and
release it afterwards.
Note that there is always a chance that "notify" will be called
before the thread starts waiting. In that case your program will
deadlock (randomly).
Jerome
--
mailto:jeberger at free.fr
http://jeberger.free.fr
Jabber: jeberger at jabber.fr
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20091021/70af1ef8/attachment.pgp>
More information about the Digitalmars-d
mailing list