Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread] getting thread id number
From: Nigel Rantor (wiggly_at_[hidden])
Date: 2009-04-25 07:37:46


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 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