|
Boost Users : |
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2007-04-03 09:14:25
On Tue, April 3, 2007 14:15, Goroll, Torsten wrote:
> Hi,
>
> I'm looking for a way, to identify a boost::thread from inside the thread. The background is, that
> I want to log into a global logger, which has to know from which thread the message was sent.
>
> I have thought at using the pointer of the class as an identifier, but that won't work if two
> threads are from the same class. Maybe it is possible to save a pointer to the member methods???
>
>
> Kind Regards
> Torsten
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi!
What about using thread specific pointer?
http://www.boost.org/doc/html/thread_specific_ptr.html
Take a look at this example:
http://www-eleves-isia.cma.fr/documentation/BoostDoc/boost_1_29_0/libs/thread/example/tss.cpp
And remember, that you can pass to your thread function any data, using smth. similar like:
############## UNTESTED CODE #################
boost::thread_specific_ptr<int> thread_id;
void thread_proc(int id)
{
thread_id.reset(new int(id)); // initialize the thread's storage
//do some stuff
}
int main(int argc, char* argv[])
{
boost::thread_group threads;
for (int i=0; i<5; ++i)
threads.create_thread( std::bind1st( std::ptr_fun(&thread_proc) , i) );
threads.join_all();
}
############## END OF UNTESTED CODE #################
With Kind Regards,
Ovanes Markarian
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