dsimcha wrote:
Well, you should lock the mutex before calling condition.notify and release it afterwards.== Quote from Bartosz Milewski ([email protected])'s articledsimcha 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 tostall 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; }
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:[email protected]
http://jeberger.free.fr
Jabber: [email protected]
signature.asc
Description: OpenPGP digital signature
