0

Can I re-join a thread after detaching from it? A while ago I read somewhere that you should call either join or detach immediately after creating a thread.

Can I detach from a thread and then re-join it later?

Example:

#include <iostream>
#include <thread>

using namespace std::chrono_literals;

int main()
{   
    auto task = []()
    {
        std::this_thread::sleep_for(3s);
        std::cout << "Job thread done" << std::endl;
    };
    
    std::thread t(task);
    t.detach(); //Detach from thread and do our own thing...
    
    //Do something
    std::this_thread::sleep_for(1s);
    std::cout << "Main thread done" << std::endl;
    
    //Wait for job thread to finish if it is not finished
    t.join();
    
    std::cout << "Everything done" << std::endl;
    
    return 0;
}
RedBox
  • 133
  • 9
  • 4
    No, that's not possible. – πάντα ῥεῖ Jun 03 '22 at 15:19
  • *you should call either join or detach immediately after creating a thread* - it looks like you remembered it incorrectly. `t.detach();` is absolutely unnecessary in your code. – 273K Jun 03 '22 at 15:22
  • 1
    If you are planing join thread somewhere in future why detach it at all? – Marek R Jun 03 '22 at 15:27
  • @RedBox The [dupe](https://stackoverflow.com/questions/37015775/what-is-different-between-join-and-detach-for-multi-threading-in-c) explains: *"`join()` cannot be called on that thread object any more(after `detach` has been used), since it is no longer associated with a thread of execution."* – Jason Jun 03 '22 at 15:28
  • 1
    In my experience, I've found `detach` to cause more problems than it is worth. The biggest danger being ungraceful (uncoordinated) thread termination at program termination. The fixes for those scenarios being careful interthread coordination, which might as well be a `join` instead. – Eljay Jun 03 '22 at 15:31
  • @AnoopRana Can I assume then that the detach() function has the sole purpose of disassociating the actual thread from the std::thrad object, so even if the thread that created the std::thread object returns and the std::thread object goes out of scope, the created thread can still run independently? – RedBox Jun 03 '22 at 15:31
  • 1
    You don't have to call `join()` or `detach()` immediately; you just have to call them *eventually*. And they're mutually exclusive. A detached thread will clean up its own resources upon completion. When two threads are joined, the main (joining) thread will wait until the secondary (joined) thread has terminated, and then it will clean up the resources. If you just store the thread object somewhere without calling either function, it will still run independently. You just have to make sure to `detach()` or `join()` it before it's destroyed so that cleanup responsibilities can be assigned. – Alexander Guyer Jun 03 '22 at 15:35
  • Re, "Can I assume then that the detach() function has the sole purpose of disassociating the actual thread from the std::thread object?" In _some_ operating systems, a dead thread continues to exist in a "zombie" state, using memory and other resources, until some other thread in the program cleans up after it. I'm _GUESSING_ that In the `std` library, that cleanup normally happens in the `join()` call, but `detach()` hands the responsibility off to some hidden part of the library by adding the thread to a container of threads that eventually will need to be cleaned up. – Solomon Slow Jun 03 '22 at 15:49
  • 1
    @RedBox -- re: "can I assume" -- no, you cannot assume anything. You can [look it up](https://en.cppreference.com/w/cpp/thread/thread/detach). – Pete Becker Jun 03 '22 at 16:36

0 Answers0