Boost logo

Boost :

Subject: Re: [boost] [thread] getting boost::thread* form boost::thread::id
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2008-11-14 07:39:45


"vicente.botet" <vicente.botet_at_[hidden]> writes:

> I'm working with a keep_alive mechanism. I need to interrupt threads
> that are considered 'dead'. I have access to the thread specific
> context using a map : boost::thread::id -> context. This mapping is
> modified by self registration from the thread. On the other side I
> need a boost::thread pointer to interrupt the thread.
>
> With the current Boost.Thread interface, I don't see how I can get it
> either from the current thread or from the thread::id. Can I?

No, you can't get a pointer or reference to the boost::thread object
for a thread from its ID. Firstly, there may not be one (e.g. the
initial thread, or a detached thread), and secondly that would defeat
the point of the separation. A boost::thread instance is a
single-ownership object. In general it is not safe to access it from
multiple threads. On the other hand, boost::thread::id values are just
values, and can be freely copied around.

Finally, if you move your thread between boost::thread objects, all
your pointers would become invalid.

Anthony

-- 
Anthony Williams
Author of C++ Concurrency in Action | http://www.manning.com/williams
Custom Software Development | http://www.justsoftwaresolutions.co.uk
Just Software Solutions Ltd, Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk