views:

70

answers:

2

Hi,

Could someone please tell us on how to print correctly the handling thread in windows? Actually I tried several ways but it doesn't return the right number as in Unix-variant, as such e.g.:

cout << " with thread " << pthread_self << endl;
cout << " with thread " << pthread_self().p << endl;

Thanks for your replies:

+1  A: 

You mean current thread handle in Windows ?

GetCurrentThread()

aJ
thans for your reply. I am getting error when using GetCurrentThread() : error C3861: 'GetCurrentThread': identifier not foundIs there library to include? thanks again
make
include Windows.h
aJ
thanks! it doesn't start from 2 as pthread-self on unix-variant. is there anythink else? thanks again
make
See Giuseppe Guerrini's comment: this doesn't return a real handle; in fact it's quite possible that this will return the same value for all threads (OS implementation detail)
MSalters
thanks a lot to you all for replies and help!
make
+2  A: 

GetCurrentThread returns a "pseudohandle", not a valid thread handle. You should use "GetCurrentThreadId" instead (or ::GetCurrentThreadId()), and include windows.h, of course.

Giuseppe Guerrini
thanks for reply. using GetCurrentThreadId doesn't start from 2 as pthread_self on Unix-variant. is there any way to do it? thanks again!
make
If you need "small IDs" as in the Unix world, I am afraid you have to add some code. A possible way is to mantain a set of zero-based "local IDs" and bind the smallest free number to the threads you create by using the TlsAlloc/TlsSetValue. Not very difficult, but quite annoying, and it works only if all the threads are created by you (i.e. not by some other module you don't control). Bye!
Giuseppe Guerrini
I'm suspecting that there's a hidden design problem if you're assuming specific values of thread identifiers. If you're on thread 508, then you're not on thread 2. Perhaps the thread ID is used as an array index instead of a hash key?
MSalters
@MSalters: by a general point of view I agree with you. But in the Unix world there is the quite general rule that the kernel objects handles are allocated by taking always the smallest free index. That's why stdin,stdout and stderr fd are 0,1 and 2. This is true in particular with file descriptors, and it seems, as "make" says, that this rule applies also to threads. Unix developers (me too) are used to advantage of this feature, but of course porting is more difficult.
Giuseppe Guerrini
I'm familiar with that Unix behavior. But that's not relevant to the question of _printing_ the correct value, is it? That's what the question seems about. If you're on thread 508, you should obviously print 508. Finding a way to print 2 is certainly doable, but senseless.
MSalters
The problem is "what is the correct value?". I suspect that the "small index" expected was interpreted as a sort of ordinal creation number ("hello, I'm thread #3, i.e. the third one"), something that Windows' thread ID doesn't implement.@make: Is my interpretation correct? Bye!
Giuseppe Guerrini
@msalters, it seems to me that you are far away from unix. I am a user of unix and on unix-variant, we can control thread as it is mentioned by @giuseppe-guerrini, (in which it is not he case on Windows). thanks a lot to you all for your replies and help!
make