If I have a shared_ptr that I copy into a std::thread, detach the thread, and then I destroy all other copies of the shared_ptr, can I expect the detached thread to continue to have a live copy of the shared_ptr? For example if I have:
{
std::shared_ptr<Foo> my_foo = std::make_shared<Foo>();
std::thread soon_detached([my_foo = my_foo]() {my_foo->bar()});
soon_detached.detach();
}
Can I trust that bar() is "safe" to call in that situation (in that it won't try to call bar on a Foo that's being destructed/is destructed even if the initial copy of my_foo is long out of scope?
foo2object more clearly: godbolt.org/z/1GrsjnxY4 - davmac