Re: getTid wrapper

2014-03-04 Thread Timothee Cour
On Tue, Mar 4, 2014 at 10:09 AM, Timothee Cour wrote:

> Thanks, that works
> does it make sense to add a function to do that?
> as it stands there are 2 separate ways,  cast(void*)Thread.getThis and
> cast(void*)getTid, so having a function would make it the preferred way.
>
>
sorry that should be:
cast(void*)Thread.getThis (works)
thisTid doesn't seem useful for purpose of writing a unique identifier (and
can't be cast to void*).
So the question is whether we want a wrapper for cast(void*)Thread.getThis.


Re: getTid wrapper

2014-03-04 Thread Ali Çehreli

On 03/04/2014 08:27 AM, Stanislav Blinov wrote:

> Perhaps that book section is indeed not clear
> enough. &thisTid actually gets an address of 'thisTid',
> which is (at least in current implementation) a function.

Makes sense. I remember thisTid being a module-level variable at the 
time I wrote that section. Taking note of one more thing to change... :)


Ali



Re: getTid wrapper

2014-03-04 Thread Stanislav Blinov

On Tuesday, 4 March 2014 at 15:52:37 UTC, Timothee Cour wrote:


quoting from there:
"The type of thisTid is Tid, but its value has no significance 
for the
program. Further, both threads report it to be surprisingly at 
the same

address:
Owner : Tid(std.concurrency.MessageBox), address: 809C360
Worker: Tid(std.concurrency.MessageBox), address: 809C360"


That part talks about thisTid, not Thread.getThis. Perhaps that 
book section is indeed not clear enough. &thisTid actually gets 
an address of 'thisTid', which is (at least in current 
implementation) a function.



Likewise with writeln(&Thread.getThis);


Same as above. So you're printing function pointers, which are of 
course the same for every thread.


How would I print a number/string that uniquely identifies a 
thread?

Tid is an opaque type, writeln(thisTid) isn't of any use.


If you want to print it, you can do so like this:

writefln("%s", cast(void*)Thread.getThis);

That is, you cast a reference returned by Thread.getThis() to a 
void* (a reference is a glorified pointer, after all). If you 
want to print something more meaningful, you can utilize the 
'name' property of Thread.


For simple identification purposes (i.e. when you want to check 
if the calling thread is the one you need), both thisTid and 
Thread.getThis can be used:


if (thisTid == expectedTid) { ... }

if (Thread.getThis is expectedThread) { ... }

If you don't feel like printing addresses or using name(), you 
can even build your own id system, i.e. like this:


-->8--

module threadid;

import core.atomic;

private shared uint lastID; // shared
private uint thisID;// TLS

static this() { thisID = atomicOp!"+="(lastID, 1); }

@property uint threadID() { return thisID; }

--8<--

...or something to that extent. Now every thread that you ever 
create will get an ID number (from 0 to uint.max, be careful 
though if your app creates more than uint.max threads :o) ), 
which can be queried at any time:


import threadid;

//...

writeln(threadID);


Re: getTid wrapper

2014-03-03 Thread Bienlein

On Tuesday, 4 March 2014 at 06:31:24 UTC, Timothee Cour wrote:


that gives tid of spawned thread, not 'self'.


The link you mentioned says it is stored in the special variable
thisTid.