D-Programming-Language/phobos — pull request #2482 by klamonte (2014-09-02 19:14:59+00:00)
Title: Issue 6989 - create a better string representation of Tid

Original discussion here: https://github.com/D-Programming-Language/phobos/pull/1582

The basic premise is that we will let Tid's expose the Thread that is receiving messages, without actually exposing the MessageBox itself. There exists a short window after spawn()/spawnLinked() where Tid.receiveThread is null (the thread hasn't yet set it in _spawn()).

I am very new to the Phobos codebase, my apologies if I am grossly breaking the process. Two areas I know this change is weak and needs help:
- Tid.toString() should be changed to not allocate.
- A proper unit test. I've got a quick tester program below that seems to work for me.

```D
import core.thread;
import std.concurrency;
import std.stdio;

void spawnTest(int threadNum) {
stdout.writefln("%d Thread.toHash(): 0x%x Tid: %s", threadNum, Thread.getThis.toHash(), thisTid);
}

void main(string [] args) {
stdout.writefln("Current thread: 0x%x", Thread.getThis.toHash());
Thread [] threads;
for (auto i = 0; i < 100; i++) {
auto child = spawn(&spawnTest, i);
stdout.writefln("[main] Tid: %s", child);
while (child.receiveThread is null) {
Thread.sleep(dur!("msecs")(1));
}
threads ~= child.receiveThread;
}
foreach (t; threads) {
t.join(false);
}
stdout.writeln("Done");
}
```