www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Thread.getThis()

reply pmoore <pmoore_member pathlink.com> writes:
Hi,

I am calling the Thread.getThis() method to return the current thread then
printing out the ptr.  I am expecting the result to be the same each time I call
the method but it isn't. Am I doing something really dumb? (It is nearly
midnight :)

import std.thread;
import std.stdio;

int main() {
Thread t1 = Thread.getThis();
Thread t2 = Thread.getThis();
Thread t3 = Thread.getThis();
Thread t4 = Thread.getThis();
writefln("thread ptr = %d",cast(uint)&t1);
writefln("thread ptr = %d",cast(uint)&t2);
writefln("thread ptr = %d",cast(uint)&t3);
writefln("thread ptr = %d",cast(uint)&t4);
return 0;
}

I am getting the following output:

thread ptr = 1244968
thread ptr = 1244972
thread ptr = 1244976
thread ptr = 1244980
Oct 14 2005
next sibling parent BCS <BCS_member pathlink.com> writes:
try:

int main() {
Thread t1 = Thread.getThis();
Thread t2 = Thread.getThis();
Thread t3 = Thread.getThis();
Thread t4 = Thread.getThis();
writefln("thread ptr = %d",cast(uint)t1);  
writefln("thread ptr = %d",cast(uint)t2);
writefln("thread ptr = %d",cast(uint)t3);
writefln("thread ptr = %d",cast(uint)t4);
return 0;
}

w/o '&' (IIRC t1 is already a pointer)


In article <dipd6r$qtr$1 digitaldaemon.com>, pmoore says...
Hi,

I am calling the Thread.getThis() method to return the current thread then
printing out the ptr.  I am expecting the result to be the same each time I call
the method but it isn't. Am I doing something really dumb? (It is nearly
midnight :)

import std.thread;
import std.stdio;

int main() {
Thread t1 = Thread.getThis();
Thread t2 = Thread.getThis();
Thread t3 = Thread.getThis();
Thread t4 = Thread.getThis();
writefln("thread ptr = %d",cast(uint)&t1);
writefln("thread ptr = %d",cast(uint)&t2);
writefln("thread ptr = %d",cast(uint)&t3);
writefln("thread ptr = %d",cast(uint)&t4);
return 0;
}

I am getting the following output:

thread ptr = 1244968
thread ptr = 1244972
thread ptr = 1244976
thread ptr = 1244980
Oct 14 2005
prev sibling parent reply JT <jtd514 ameritech.net> writes:
I think you are getting the address of the references.


pmoore wrote:
 Hi,
 
 I am calling the Thread.getThis() method to return the current thread then
 printing out the ptr.  I am expecting the result to be the same each time I
call
 the method but it isn't. Am I doing something really dumb? (It is nearly
 midnight :)
 
 import std.thread;
 import std.stdio;
 
 int main() {
 Thread t1 = Thread.getThis();
 Thread t2 = Thread.getThis();
 Thread t3 = Thread.getThis();
 Thread t4 = Thread.getThis();
 writefln("thread ptr = %d",cast(uint)&t1);
 writefln("thread ptr = %d",cast(uint)&t2);
 writefln("thread ptr = %d",cast(uint)&t3);
 writefln("thread ptr = %d",cast(uint)&t4);
 return 0;
 }
 
 I am getting the following output:
 
 thread ptr = 1244968
 thread ptr = 1244972
 thread ptr = 1244976
 thread ptr = 1244980
 
 
Oct 14 2005
parent Niko Korhonen <niktheblak hotmail.com> writes:
JT wrote:
 I think you are getting the address of the references.
Exactly. Each call of the Thread.getThis() function returns a new instance of class Thread, which only /represents/ a thread and is not a naked pointer to a real thread or anything. So you have several instances of class Thread which all represent the same actual thread. Therefore the addresses of the references are different. Niko Korhonen SW Developer
Oct 15 2005