I need a way to get the Tid of the thread that spawned the current thread. I've had this kind of bug creep into my code: __gshared Tid mainThread; __gshared Tid workThread; workThreadFoo() { workThread.send("data"); // Bug: Meant to be mainThread.send } mainThreadFoo() { mainThread = thisTid; workThread = spawn(&workThreadFoo); } This can be taken care of with this workaround: workThreadFoo() { Tid mainThread = receiveOnly!Tid(); // workThread.send("data"); // Now this can't creep in mainThread.send("data"); // correct } mainThreadFoo() { Tid workThread = spawn(&workThreadFoo); workThread.send(thisTid); } But it would be better if I didn't have to take this extra step and instead used: workThreadFoo() { Tid mainThread = thisTid.ownerTid; // new read-only property mainThread.send("data"); // correct thisTid.ownerTid.send("data2"); // also correct } mainThreadFoo() { Tid workThread = spawn(&workThreadFoo); }
I forgot I can also use spawn to send the Tid, e.g.: void workThreadFoo(Tid mainThread) { } spawn(&workThreadFoo, thisTid); It would still be nice to have a ownerTid property though, this is a feature request. :)
Ok apparently there's a global but private 'owner' Tid. The problem is this is equal to Tid.init for the main thread since Tid is a struct. I don't think it's wise to simply return: /** * Return the Tid of the thread which * spawned the caller's thread. */ @property Tid ownerTid() { return owner; } As calling ownerTid.send() will segfault due to mbox being null. So what should be done instead, maybe throw an exception in ownerTid() if mbox is null?
(In reply to comment #2) > The problem is this is > equal to Tid.init for the main thread since Tid is a struct. That wasn't worded properly. It's .init because it doesn't have a parent, regardless of whether it's a struct or class.
https://github.com/D-Programming-Language/phobos/pull/1092
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/dd9b31ccaef70915c7a2b18b05d9f699fbb4ae5f Fixes Issue 6224 - Add ownerTid() property. https://github.com/D-Programming-Language/phobos/commit/9cb2065fe6f2302a3c47f1aff3e005de5c90c110 Merge pull request #1092 from AndrejMitrovic/Fix6224 Issue 6224 - Add ownerTid() property.