Do threads 'end' themselves using core.thread?

Alex Horvat alexh at gmail.com
Sat Jul 20 18:04:36 PDT 2013


On Saturday, 20 July 2013 at 20:36:29 UTC, Ali Çehreli wrote:
> 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

Thanks.

What happens if I don't call thread join? (My thread is just a 
timer to hide a bit of text, so I just create it, forget about it 
and continue with the main thread)
Will the thread still dispose of itself in this situation?




More information about the Digitalmars-d-learn mailing list