Boost logo

Boost Users :

Subject: Re: [Boost-users] Get "Native" thread id - feature request
From: Zachary Turner (divisortheory_at_[hidden])
Date: 2009-11-05 11:11:26


On Thu, Nov 5, 2009 at 9:02 AM, Ondrej Sluciak <
ondrej.sluciak_at_[hidden]> wrote:

>
> Hello, is there a way how to get the thread id that system assigns to
> thread? I mean the id that is located in "thread_info->id". It would quite
> nice if boost could return this value, because for example if I want to set
> name of the thread on windows using function SetThreadName(), I need exactly
> that id. In my application I would have to add this "feature" directly to
> boost::thread.hpp, which is not very nice solution.
> I know that also GetThreadName(myThread->native_handle()) should work, but
> if I do it like this I always get a Windows error "The procedure entry point
> GetThreadId could not be located in the dynamic link library KERNEL32.dll".
> And I also guess, that this new "feature" wouldn't destroy the overall
> design of boost::thread architecture. To get the "real" thread id is in my
> opinion in general a useful thing.
>

The reason is that GetThreadId only exists on 2003/Vista+. There is an
undocumented way to do what you want, and it requires using
NtQueryInformationThread which is undocumented. To use it you need to
manually LoadLibrary / GetProcAddress. The output parameter of this
function is another undocumented structure which contains the thread id.
Nevertheless, look into it and I think you'll find at least something that
works, even if it's ugly.

That being said, I agree the thread id is important sometimes, if we have
ability to get the handle there's no reason we shouldn't have the ability to
get the id also.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net