Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Can I use a semaphore this way?

Moderators: larsivi kris

Posted: 10/27/07 20:42:16

I have a bunch of worker threads, and one controlling thread. I want the controller thread to either wait for all workers to finish or a specified time delay (whichever comes first).

Looking at tango.core.sync, I think my best bet is a semaphore. Can I initialize it with a negative number (with 5 work threads, I'd initialize it to -4). Can I then have worker threads call notify to increase the counter and have the controlling thread use wait(time)? Some of the semaphore docs seem to explicitly state that a value of zero is unlocked (tryWait).

Either way, when the controlling thread is done waiting, it'll stop all the workers.

Author Message

Posted: 10/27/07 22:35:56 -- Modified: 10/27/07 22:39:27 by
kris -- Modified 2 Times

You might find that a ThreadGroup? would do the job? If you create all the workers as part of a group, then the 'host' can do a group.joinAll() ...

Posted: 10/28/07 03:35:31

I don't think that'd work for me. I don't expect the worker threads to end (just pause periodically). joinAll seems to require the threads to end. Even if I did have them end, joinAll has no timelimit.

Posted: 10/28/07 04:04:58

Yeah. Looks like there's nothing in tango.core.sync to do this, so I'd suggest creating a ticket and assigning it to Sean

Posted: 10/29/07 02:59:47

This is now ticket #708