Thread pools

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 22 10:01:50 PDT 2015


On Wednesday, 22 July 2015 at 16:16:36 UTC, John Colvin wrote:
> On Wednesday, 22 July 2015 at 15:51:23 UTC, Chris wrote:
>> On Wednesday, 22 July 2015 at 15:41:06 UTC, Alex Parrill wrote:
>>> [...]
>>
>> Thanks. I'm dealing with "nested" threads at the moment.
>>
>> main
>> {
>>   spawn(thread1)
>>   {
>>     // Does some processing
>>     spawn(thread2)
>>     {
>>       // Plays audio
>>     }
>>   }
>> }
>>
>> If main receives a signal, all threads should stop immediately 
>> (thread1 and thread2).
>
> I would send a message to terminate to thread1, which would in 
> turn send a similar message to any threads it has started, wait 
> until they've all stopped (maybe with a time-out), then return.
>
> I.e. every thread knows how to cleanly terminate itself when 
> instructed, so you just send a terminate message down the tree 
> of threads and then wait for the effects to bubble back up to 
> main.

You can probably simply terminate the main thread, which will 
send an OwnerTerminated message to all dependent threads. The 
threads need to `receive()` this message and terminate.


More information about the Digitalmars-d-learn mailing list