Do threads 'end' themselves using core.thread?

Ali Çehreli acehreli at yahoo.com
Sat Jul 20 13:36:24 PDT 2013


On 07/20/2013 12:34 PM, Alex Horvat wrote:

 > If I use core.thread.Thread to create a new thread associated to a
 > function like this:
 >
 > Thread testThread = new Thread(&DoSomething);
 >
 > Will the testThread dispose of itself after DoSomething() completes, or
 > do I need to join/destroy/somethingelse testThread?

When the execution of the thread finishes, you will end up with a 
completed thread:

import std.stdio;
import core.thread;

void DoSomething()
{
     writeln("Doing something...");
}

void main()
{
     auto thread = new Thread(&DoSomething);
     thread.start();

     thread_joinAll();

     assert(!thread.isRunning);    // <-- Not running
}

You do not need to do anything with the object. In fact, the 
documentation says that you shouldn't delete it: "instances of this 
class should never be explicitly deleted". ('delete' is gone, so it 
should mean that you shouldn't destroy() it.)

Ali



More information about the Digitalmars-d-learn mailing list