digitalmars.D.learn - getTid wrapper
- Timothee Cour (8/8) Mar 03 2014 I couldn't find a wrapper to get current thread id.
- Bienlein (3/13) Mar 03 2014 The spawn function returns the tid, see the sample here:
- Timothee Cour (2/16) Mar 03 2014 that gives tid of spawned thread, not 'self'.
- Bienlein (3/4) Mar 03 2014 The link you mentioned says it is stored in the special variable
- Stanislav Blinov (8/13) Mar 04 2014 Where exactly? I couldn't find it, but if Ali's book states that
- Timothee Cour (19/36) Mar 04 2014 quoting from there:
- Stanislav Blinov (35/47) Mar 04 2014 That part talks about thisTid, not Thread.getThis. Perhaps that
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/7) Mar 04 2014 Makes sense. I remember thisTid being a module-level variable at the
- ollie (9/14) Mar 04 2014 I ran into this problem with threads. You can't just take a pointer to a
- Timothee Cour (6/10) Mar 04 2014 sorry that should be:
- Timothee Cour (5/20) Mar 04 2014 Thanks, that works
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
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
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:that gives tid of spawned thread, not 'self'.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
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
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 threadsThat 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 Threadcore.thread.Thread.getThis(); std.concurrency.thisTid; The latter one is mentioned in that same chapter.
Mar 04 2014
On Tue, Mar 4, 2014 at 2:34 AM, Stanislav Blinov <stanislav.blinov gmail.comwrote:On Tuesday, 4 March 2014 at 04:39:33 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" Likewise with writeln(&Thread.getThis);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 threadsThat 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 ThreadHow 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; }core.thread.Thread.getThis(); std.concurrency.thisTid; The latter one is mentioned in that same chapter.
Mar 04 2014
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
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
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
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
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)soit'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