Bug 6224 – Add an ownerTid property in std.concurrency
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2011-06-29T14:39:00Z
Last change time
2013-03-08T09:55:22Z
Keywords
pull
Assigned to
andrej.mitrovich
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2011-06-29T14:39:31Z
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);
}
Comment #1 by andrej.mitrovich — 2011-06-29T16:11:36Z
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. :)
Comment #2 by andrej.mitrovich — 2013-01-22T15:22:35Z
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?
Comment #3 by andrej.mitrovich — 2013-01-22T15:24:33Z
(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.
Comment #4 by andrej.mitrovich — 2013-01-22T15:59:50Z