www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - getTid wrapper

reply Timothee Cour <thelastmammoth gmail.com> writes:
I couldn't find a wrapper to get current thread id.
There is Thread.getThis but the address it returns is the same for all
threads as explained here (http://ddili.org/ders/d.en/concurrency.html) so
it's not useful for logging.

How about adding this:
auto getTid() to class Thread
Implementation on OSX:
mach_port_t tid = pthread_mach_thread_np(pthread_self());
Mar 03 2014
next sibling parent reply "Bienlein" <jeti789 web.de> writes:
On Tuesday, 4 March 2014 at 04:39:33 UTC, Timothee Cour wrote:
 I couldn't find a wrapper to get current thread id.
 There is Thread.getThis but the address it returns is the same 
 for all
 threads as explained here 
 (http://ddili.org/ders/d.en/concurrency.html) so
 it's not useful for logging.

 How about adding this:
 auto getTid() to class Thread
 Implementation on OSX:
 mach_port_t tid = pthread_mach_thread_np(pthread_self());
The spawn function returns the tid, see the sample here: http://dlang.org/phobos/std_concurrency.html
Mar 03 2014
parent reply Timothee Cour <thelastmammoth gmail.com> writes:
On Mon, Mar 3, 2014 at 10:19 PM, Bienlein <jeti789 web.de> wrote:

 On Tuesday, 4 March 2014 at 04:39:33 UTC, Timothee Cour wrote:

 I couldn't find a wrapper to get current thread id.
 There is Thread.getThis but the address it returns is the same for all
 threads as explained here (http://ddili.org/ders/d.en/concurrency.html)
 so
 it's not useful for logging.

 How about adding this:
 auto getTid() to class Thread
 Implementation on OSX:
 mach_port_t tid = pthread_mach_thread_np(pthread_self());
The spawn function returns the tid, see the sample here: http://dlang.org/phobos/std_concurrency.html
that gives tid of spawned thread, not 'self'.
Mar 03 2014
parent "Bienlein" <jeti789 web.de> writes:
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.
Mar 03 2014
prev sibling next sibling parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Tuesday, 4 March 2014 at 04:39:33 UTC, Timothee Cour wrote:
 I couldn't find a wrapper to get current thread id.
 There is Thread.getThis but the address it returns is the same 
 for all threads
That is not true.
 as explained here (http://ddili.org/ders/d.en/concurrency.html)
Where exactly? I couldn't find it, but if Ali's book states that Thread.getThis() returns the same reference, it's a bug in the book that should be fixed.
 auto getTid() to class Thread
core.thread.Thread.getThis(); std.concurrency.thisTid; The latter one is mentioned in that same chapter.
Mar 04 2014
parent reply Timothee Cour <thelastmammoth gmail.com> writes:
On Tue, Mar 4, 2014 at 2:34 AM, Stanislav Blinov <stanislav.blinov gmail.com
 wrote:
 On Tuesday, 4 March 2014 at 04:39:33 UTC, Timothee Cour wrote:

 I couldn't find a wrapper to get current thread id.
 There is Thread.getThis but the address it returns is the same for all
 threads
That is not true. as explained here (http://ddili.org/ders/d.en/concurrency.html)

 Where exactly?
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" Likewise with writeln(&Thread.getThis);
 I couldn't find it, but if Ali's book states that Thread.getThis() returns
 the same reference, it's a bug in the book that should be fixed.


  auto getTid() to class Thread

 core.thread.Thread.getThis();
 std.concurrency.thisTid;

 The latter one is mentioned in that same chapter.
How would I print a number/string that uniquely identifies a thread? Tid is an opaque type, writeln(thisTid) isn't of any use. struct Tid { private: this( MessageBox m ) { mbox = m; } MessageBox mbox; }
Mar 04 2014
parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
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);
Mar 04 2014
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Mar 04 2014
prev sibling parent reply ollie <ollie home.net> writes:
On Mon, 03 Mar 2014 20:39:17 -0800, Timothee Cour wrote:

 I couldn't find a wrapper to get current thread id.
 There is Thread.getThis but the address it returns is the same for all
 threads as explained here (http://ddili.org/ders/d.en/concurrency.html) so
 it's not useful for logging.
 
I ran into this problem with threads. You can't just take a pointer to a thread (ie &Thread.getThis). You have to use a cast(void*) for it to work. I put the following print statement where I created the thread and in the created thread: writefln("Thread %X is%s the main thread.", cast(void*)Thread.getThis, thread_isMainThread ? "" : "n't"); This worked, otherwise using '&' to get the pointer gave the same address. ollie
Mar 04 2014
next sibling parent Timothee Cour <thelastmammoth gmail.com> writes:
On Tue, Mar 4, 2014 at 10:09 AM, Timothee Cour <thelastmammoth gmail.com>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.
Mar 04 2014
prev sibling parent Timothee Cour <thelastmammoth gmail.com> writes:
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.



On Tue, Mar 4, 2014 at 9:42 AM, ollie <ollie home.net> wrote:

 On Mon, 03 Mar 2014 20:39:17 -0800, Timothee Cour wrote:

 I couldn't find a wrapper to get current thread id.
 There is Thread.getThis but the address it returns is the same for all
 threads as explained here (http://ddili.org/ders/d.en/concurrency.html)
so
 it's not useful for logging.
I ran into this problem with threads. You can't just take a pointer to a thread (ie &Thread.getThis). You have to use a cast(void*) for it to work. I put the following print statement where I created the thread and in the created thread: writefln("Thread %X is%s the main thread.", cast(void*)Thread.getThis, thread_isMainThread ? "" : "n't"); This worked, otherwise using '&' to get the pointer gave the same address. ollie
Mar 04 2014