[thread] getting thread id number
Hi! I'm trying to get the id number of a thread just to write it to the standard output. Since I'm not an expert in c++ programming, let me explain what I do (even if it's quite simple): a create an instance as follows, boost::thread::id thread_id= boost::this_thread::get_id(); By debugging my code with Visual Studio, I see that 'thread_id' has an 'unsigned int' object member called 'id', so I would like to display this value. Unfortunately, I cannot find in the boost documentation any function member of boost::thread::id class that returns its value. Thanks, Hipatia. -- View this message in context: http://www.nabble.com/-thread--getting-thread-id-number-tp23222717p23222717.... Sent from the Boost - Users mailing list archive at Nabble.com.
On Apr 24, 2009, at 11:47 AM, Hipatia wrote:
Hi!
I'm trying to get the id number of a thread just to write it to the standard output.
Since I'm not an expert in c++ programming, let me explain what I do (even if it's quite simple): a create an instance as follows, boost::thread::id thread_id= boost::this_thread::get_id(); By debugging my code with Visual Studio, I see that 'thread_id' has an 'unsigned int' object member called 'id', so I would like to display this value. Unfortunately, I cannot find in the boost documentation any function member of boost::thread::id class that returns its value.
The id class defined operator<<, so just do this: std::cout << "Thread id is: " << boost::this_thread::get_id(); I just did a replacement of a pthread implementation with a boost thread implementation, and I needed to get the numeric value of the data member, which I did like this (Thread::id_t is a typedef for uint64_t): Thread::id_t getId() { 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. Stupid. Just stupid. void *numericId; ios << this_thread::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; } It's a hack, I know, but I couldn't think of anything else. - Rush
Actually I may have simplified my request too much. At first, I used to do what you propose me, that is, std::cout << "Thread id is: " << boost::this_thread::get_id(); But this command gives back the address of the 'thread_data' object rather than the actual thread id number. Still, it is a good solution. But since I create several threads each one writing to the standard ouput, I need a mutex-based function that centralizes all the writing: void writeout(std::string message); (this function not only writes the thread's id number but also other information) So now I need the thread id converted into a string :( Any suggestion? Thanks, Hipatia Rush Manbert wrote:
On Apr 24, 2009, at 11:47 AM, Hipatia wrote:
Hi!
I'm trying to get the id number of a thread just to write it to the standard output.
Since I'm not an expert in c++ programming, let me explain what I do (even if it's quite simple): a create an instance as follows, boost::thread::id thread_id= boost::this_thread::get_id(); By debugging my code with Visual Studio, I see that 'thread_id' has an 'unsigned int' object member called 'id', so I would like to display this value. Unfortunately, I cannot find in the boost documentation any function member of boost::thread::id class that returns its value.
The id class defined operator<<, so just do this:
std::cout << "Thread id is: " << boost::this_thread::get_id();
I just did a replacement of a pthread implementation with a boost thread implementation, and I needed to get the numeric value of the data member, which I did like this (Thread::id_t is a typedef for uint64_t):
Thread::id_t getId() { 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. Stupid. Just stupid. void *numericId;
ios << this_thread::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; }
It's a hack, I know, but I couldn't think of anything else.
- Rush _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/-thread--getting-thread-id-number-tp23222717p23229970.... Sent from the Boost - Users mailing list archive at Nabble.com.
Hipatia wrote:
Actually I may have simplified my request too much. At first, I used to do what you propose me, that is, std::cout << "Thread id is: " << boost::this_thread::get_id(); But this command gives back the address of the 'thread_data' object rather than the actual thread id number. Still, it is a good solution.
But since I create several threads each one writing to the standard ouput, I need a mutex-based function that centralizes all the writing: void writeout(std::string message); (this function not only writes the thread's id number but also other information) So now I need the thread id converted into a string :(
I think you may misunderstand the idea behind the thread_id object. The thread_id returned is only useful to *identify* threads. It provides a full set of comparison operators but doesn't actually provide *any* user-visible data members. In other words the "thread id number" you are talking about simply does not exist. The boost thread library is multiplatform, if you look deeper into the source you'll see that the thread_data definition depends on the platform so if boost were to support yet another threading model in the future you may not see the overloaded << operator printing the address of the thread local storage but something else. If you *want* or *need* some way of mapping a thread_id object to some integral value then you can maintain a map of thread_id --> int, the thread_id instance can be used as a key because of the ordering guarantees it provides. Hope that makes sense, Nigel
Hi again! It's quite possible that I may have been focusing on the wrong class. Nonetheless... let me show you what I see when debugging with Visual Studio. If I create an object like: boost::thread::id thread_id= boost::this_thread::get_id(); I see this in the watch window: thread_id |-> thread_data |-> _p |-> [boost::detailt....] |-> _vfptr |-> count |-> thread_handle |-> interruption_handle |-> thread_exit_callbacks |-> tss_data |-> interruption_enabled |-> id This last parameter (an unsigned integer) is the one that I want to retrieve. By the way, in Visual Studio there is a thread watch window in which I can see the current existing threads and their associated ID numbers. Thus, both the id shown in this window and the one shown in the thread_id properties are the same. Nigel Rantor wrote:
Hipatia wrote:
Actually I may have simplified my request too much. At first, I used to do what you propose me, that is, std::cout << "Thread id is: " << boost::this_thread::get_id(); But this command gives back the address of the 'thread_data' object rather than the actual thread id number. Still, it is a good solution.
But since I create several threads each one writing to the standard ouput, I need a mutex-based function that centralizes all the writing: void writeout(std::string message); (this function not only writes the thread's id number but also other information) So now I need the thread id converted into a string :(
I think you may misunderstand the idea behind the thread_id object.
The thread_id returned is only useful to *identify* threads.
It provides a full set of comparison operators but doesn't actually provide *any* user-visible data members.
In other words the "thread id number" you are talking about simply does not exist.
The boost thread library is multiplatform, if you look deeper into the source you'll see that the thread_data definition depends on the platform so if boost were to support yet another threading model in the future you may not see the overloaded << operator printing the address of the thread local storage but something else.
If you *want* or *need* some way of mapping a thread_id object to some integral value then you can maintain a map of thread_id --> int, the thread_id instance can be used as a key because of the ordering guarantees it provides.
Hope that makes sense,
Nigel _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/-thread--getting-thread-id-number-tp23222717p23236018.... Sent from the Boost - Users mailing list archive at Nabble.com.
|-> id This last parameter (an unsigned integer) is the one that I want to retrieve.
By the way, in Visual Studio there is a thread watch window in which I can see the current existing threads and their associated ID numbers. Thus, both the id shown in this window and the one shown in the thread::id properties are the same.
This is implementation detail that boost.thread doesn't expose. But as
already proposed, you can make std::map
----- Original Message -----
From: "Hipatia"
Hi!
I'm trying to get the id number of a thread just to write it to the standard output.
Since I'm not an expert in c++ programming, let me explain what I do (even if it's quite simple): a create an instance as follows, boost::thread::id thread_id= boost::this_thread::get_id(); By debugging my code with Visual Studio, I see that 'thread_id' has an 'unsigned int' object member called 'id', so I would like to display this value. Unfortunately, I cannot find in the boost documentation any function member of boost::thread::id class that returns its value.
Hi, have you tried with the native_hadle() function. One major caveat: you can not revover directly the current boost::thread using the interface provided by the library,incroyable, isn't it? So you will need to maintain a map from boost::thread::id to boost::thread* or native_handle_type HTH, Vicente
participants (5)
-
Hipatia
-
Igor R
-
Nigel Rantor
-
Rush Manbert
-
vicente.botet