[thread] getting boost::thread* form boost::thread::id

Hi Anthony, 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? Currently only the thread creator wrapper can add this information. I think that I could add a thread creator wrapper that add this as a thread specific pointer, but I will be able to get only the thread* for the threads created with this wrapper, which is not completly satisfactory. Do you think that at least one of these functions could be of general use: one for getting a boost::thread* form a boost::thread::id and one for getting the boost::thread* of the current thread? namespace boost { class thread { // ... static thread* get_thread_ptr(id); }; namespace this thread { thread* get_thread_ptr(); } } To implement this in Boost.Thread it will be need to store the thread* on the detail::thread_data_ptr. Do you think that this could be a possible addition on the Boost.Thread library? Thanks, Vicente

"vicente.botet" <vicente.botet@wanadoo.fr> 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

Anthony Williams-4 wrote:
"vicente.botet" <vicente.botet@wanadoo.fr> writes:
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.
If a boost::thread instance is a single-ownership object, what about thread::group? Is the ownership of the thread added to a group transfered to the group? No, I don't think so. In which cases it is not safe to access it from multiple threads? Well even if you are right and the thread pointer was not a good idea, as what I want is to interrupt a thread for which I have a thread::id may be you can add a static function void interrupt(thread::id); Is this function unsafe? Vicente -- View this message in context: http://www.nabble.com/-thread--getting-boost%3A%3Athread*-form-boost%3A%3Ath... Sent from the Boost - Dev mailing list archive at Nabble.com.
participants (3)
-
Anthony Williams
-
Vicente Botet Escriba
-
vicente.botet