Yes, if you have all the thread objects, you can find one that thread::id is equal to this_thread::get_id() , and you can call detach() on it and destroy the thread object after that (C ++ 11 standard prevents this, and I believe that it is based on common practice). Make sure that no other thread of execution refers to the std::thread instance that was destroyed.
But you cannot call join() from the thread itself: trying to bind the thread to yourself will lead to a deadlock, and C ++ 11 implementations should recognize this and throw a system_error with the error condition resource_deadlock_would_occur .
Alternatively, you can leave a message (for example, via std :: atomic variable) in the main thread, which the thread associated with a particular instance of std::thread is about to complete, and let the main thread connect to this instance at a later point .
Alexey Kukanov
source share