This is the mail archive of the mailing list for the pthreas-win32 project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Accessing Windows thread id

Michael Bieber wrote:

I would like exploit the technique, described here:

in order to help my Visual Studio debugger with some more expressive
thread names.

Unfortunately, this is relying on a thread id not being public
accessible for pthreads_win32 clients (It is the last argument to
_beginthreadex, after that call hidden inside the non-public
ptw32_thread_t structure). Windows GetThreadId is no help here, because
it isn't defined for Win XP. Even GetCurrentId is not entirely what I
would like, because the call to SetThreadName (cf. URL above) doesn't
come from the calling thread in my case.

So, is there a way to get the Windows thread id in a way similar to
pthread_getw32threadhandle_np for a handle?
Not nicely but otherwise, yes.

As you've found, the win32 thread ID returned by the last arg of _beginthreadex() is stored at offset zero in the opaque pthw32_thread_t_ struct, which is pointed to by the first element (offset zero) of the pthread_t struct, a pointer to which is the first arg passed to pthread_create(). So the code below should get you close if not all the way there:-

DWORD w32threadID;
pthread_t tid;
pthread_create(&tid, ...);
w32threadID = (DWORD) ((void *)tid)[0][0];

I'll add another function to the library to return this nicely - called, say, pthread_getw32threadid_np().


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]