
On Mar 27, 2007, at 1:44 PM, Peter Dimov wrote:
New use case:
vector<thread> v; ... fill with threads ... for (auto i = v.begin(), e = v.end(); i != e; ++i) if (i_feel_like_it) i->detach(); ... // need new thread now, go look for a detached one and use it auto i = v.begin(); for (auto e = v.end(); i != e; ++i) if (i->is_detached()) // N2184 spells this i->get_id() == thread::id() break; if (i != v.end()) *i = std::thread(f); // recycle detached thread else v.push_back(std::thread(f));
I was thinking of something along these lines, but it still seems contrived to me. There is no reason to reuse an std::thread _object_ because you gain no efficiency from that (as opposed to reusing a thread).
What if the above use case uses vector<MyClass> where MyClass contains a lot of expensive resources (or other data that needs to be kept around for one reason or another) *plus* a std::thread? if (i != v.end()) i->install_new_thread(std::thread(f)); I.e. the fact that the thread is recycled instead of erased and reconstructed here is due to reasons which have nothing to do with with std::thread itself. -Howard