Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread] getting thread id number
From: Hipatia (hipatia_370_at_[hidden])
Date: 2009-04-25 16:15:35


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_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>

-- 
View this message in context: http://www.nabble.com/-thread--getting-thread-id-number-tp23222717p23236018.html
Sent from the Boost - Users mailing list archive at Nabble.com.

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