www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get parent Tid of a thread?

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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
}
Jun 29 2011
next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
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
Jun 29 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
I've filed it http://d.puremagic.com/issues/show_bug.cgi?id=6224
Jun 29 2011
prev sibling parent Ali =?iso-8859-1?q?=C7ehreli?= <acehreli yahoo.com> writes:
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
Jun 29 2011