|
Boost Users : |
Subject: Re: [Boost-users] Get "Native" thread id - feature request
From: Rush Manbert (rush_at_[hidden])
Date: 2009-11-05 13:19:35
On Nov 5, 2009, at 7:02 AM, Ondrej Sluciak 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.
I'm almost ashamed to post this, but maybe it helps you.
Here's a really nasty hack that I came up with to get the Thread::id_t
value of the thread id:
Thread::id_t getId() {
// This is awful. The Boost::thread implementation returns a
thread_id class.
// This is the only way to extract the actual value it carries,
which is what a Thread::id_t
// wants to be. In their Windows implementation, get_id() just
returns an int, so we wouldn't
// need this junk.
std::stringstream ios;
// This needs to be a pointer because on a *nix system,
this_thread::get_id()
// returns an object that defines operator<< to write the
pthread_t to the stream,
// and it's a pointer, so it gets formatted as hex and we can't
change that by
// inserting a dec manipulator. So when we read out of the
stream, we need to
// read into something for which a hex format makes sense.
void *numericId;
// If this is called before start(), we're hosed
assert (bthread_);
ios << bthread_->get_id();
ios >> numericId;
// And, of course, static_cast can't be used to convert from
void* to the integer type.
return (Thread::id_t)numericId;
}
- Rush
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