How are theads, Tid and spawn related?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 1 21:59:02 PDT 2014


On 11/01/2014 04:32 PM, Neven wrote:
> Ok, a newbie question ahead. I want to create new thread which calls
> given function with some parameters. Thus, I think spawn is the right
> function for me. However that functions returns Tid and not a Thread
> object.
>
> So I want to know how can I make a Thread object out of it. What I would
> like to achieve is to wait for spawned thread to finish its execution,
> that is join with main thread.

You don't need a Thread object to wait for a thread to finish. One 
option is to wait for a specific message:

import std.concurrency;

struct Done
{}

void func(int i)
{
     ownerTid.send(Done());
}

void main()
{
     spawn(&func, 42);
     receiveOnly!Done();    // <-- Waiting for the Done message
}

Another option is to wait for all child threads by thread_joinAll():

import core.thread;
// ...
     thread_joinAll();

The reason I suggest the above is because I don't know the answer to 
your question. :)

Ali



More information about the Digitalmars-d-learn mailing list