Close Threads

Ali Çehreli acehreli at yahoo.com
Mon May 28 18:59:43 UTC 2018


On 05/28/2018 11:47 AM, candle wrote:
> Hey Folks.
> 
> I have a question about threading and how to close threads i don't need 
> anymore.
> 
> In the following code, i build 3 threads with spawn from the concurrency 
> module.
> 
> if one thread found the number, all others thread must stop search.
> 
> but i have a big logical problem in my mind.
> 
> i hope someone can help me to find a solution.
> 
> Happy Greetings
> 
> and here is the code:
> https://pastebin.com/vQRediG6

The problem is, the threads are all in their while loops, never checking 
'finish' message.

You need to move the 'receive(bool finish)' checking inside the while 
loop but you must replace it with receiveTimeout with 0 duration, 
otherwise your receive call will block until a message arrives:

   receiveTimeout(0.msecs, (bool finish) {
         if(finish == true){
             return;
         }
     });

Ali


More information about the Digitalmars-d-learn mailing list