D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6224 - Add an ownerTid property in std.concurrency
Summary: Add an ownerTid property in std.concurrency
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: Other All
: P2 enhancement
Assignee: Andrej Mitrovic
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2011-06-29 14:39 UTC by Andrej Mitrovic
Modified: 2013-03-08 09:55 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2011-06-29 14:39:31 UTC
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 Andrej Mitrovic 2011-06-29 16:11:36 UTC
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 Andrej Mitrovic 2013-01-22 15:22:35 UTC
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 Andrej Mitrovic 2013-01-22 15:24:33 UTC
(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 5 github-bugzilla 2013-03-08 09:53:55 UTC
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.