Thread overview
Get parent Tid of a thread?
Jun 29, 2011
Andrej Mitrovic
Jun 29, 2011
Simen Kjaeraas
Jun 29, 2011
Andrej Mitrovic
Jun 29, 2011
Ali Çehreli
June 29, 2011
Is there any way a newly spawned thread can get the Tid of the thread that spawned it, basically its parent? I'd prefer that over using this:

__gshared mainThread; // so workThread can access it

{
mainThread = thisTid();
auto workThread = spawn(&MidiThread);  // local
}
June 29, 2011
On Wed, 29 Jun 2011 22:59:47 +0200, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:

> Is there any way a newly spawned thread can get the Tid of the thread
> that spawned it, basically its parent? I'd prefer that over using
> this:
>
> __gshared mainThread; // so workThread can access it
>
> {
> mainThread = thisTid();
> auto workThread = spawn(&MidiThread);  // local
> }

std.concurrency actually has a thread-local variable called 'owner',
which is exactly  what you want. However, it is private.

This may very well be worth an enhancement request.

-- 
  Simen
June 29, 2011
I've filed it http://d.puremagic.com/issues/show_bug.cgi?id=6224
June 29, 2011
On Wed, 29 Jun 2011 22:59:47 +0200, Andrej Mitrovic wrote:

> Is there any way a newly spawned thread can get the Tid of the thread that spawned it, basically its parent? I'd prefer that over using this:
> 
> __gshared mainThread; // so workThread can access it
> 
> {
> mainThread = thisTid();
> auto workThread = spawn(&MidiThread);  // local }

Just pass it in as the first parameter:

import std.stdio;
import std.concurrency;

void workerThread(Tid owner)
{
    // ...

    writeln("worker's thisTid: ", &thisTid);
    assert(owner != thisTid);
}

void main()
{
    writeln("owner's thisTid : ", &thisTid);
    Tid worker = spawn(&workerThread, thisTid);
}

Interestingly the two variables will have the same address but they are not equal as the assert above passes:

owner's thisTid : 46B58C
worker's thisTid: 46B58C

Ali