Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is different between join() and detach() for multi threading in C++?

What is different between join() and detach() in multi threading in C++? Does join() kill the thread?

like image 903
r.hg Avatar asked May 03 '16 22:05

r.hg


People also ask

Can a detached thread be joined?

5.6 [ISO/IEC 9899:2011], states that a thread shall not be joined once it was previously joined or detached. Similarly, subclause 7.26.

What is thread detach in C?

Allows storage for the thread whose thread ID is in the location thread to be reclaimed when that thread ends. This storage is reclaimed on process exit, regardless of whether the thread was detached, and may include storage for thread's return value.

What happens if thread join is not called?

Yes, it is safe insofar as the thread will continue until completion by itself, you do not need to have a reference to the Thread object to keep it alive, nor will the behavior of the thread change in any way.

What is join in thread C++?

The C++ thread join is used to blocks the threads until the first thread execution process is completed on which particular join() method is called to avoid the misconceptions or errors in the code. If suppose we are not using any join() method in the C++ code.


2 Answers

A C++ thread object generally (but not always) represents a thread of execution, which is an OS or platform concept.

When thread::join() is called, the calling thread will block until the thread of execution has completed. Basically, this is one mechanism that can be used to know when a thread has finished. When thread::join() returns, the OS thread of execution has completed and the C++ thread object can be destroyed.

The thread::detach() is called, the thread of execution is "detached" from the thread object and is no longer represented by a thread object - they are two independent things. The C++ thread object can be destroyed and the OS thread of execution can continue on. If the program needs to know when that thread of execution has completed, some other mechanism needs to be used. join() cannot be called on that thread object any more, since it is no longer associated with a thread of execution.

It is considered an error to destroy a C++ thread object while it is still "joinable". That is, in order to destroy a C++ thread object either join() needs to be called (and completed) or detach() must be called. If a C++ thread object is still joinable when it's destroyed, an exception will be thrown.

Some other ways that a C++ thread object will not represent a thread of execution (ie., can be unjoinable):

  • A default constructed thread object does not represent a thread of execution, so is not joinable.
  • A thread that has been moved from will no longer represent a thread of execution, so is not joinable.
like image 77
Michael Burr Avatar answered Sep 21 '22 08:09

Michael Burr


join() doesn't kill the thread. Actually it waits until thread main function returns. So if your thread main function looks like this:

while (true) { } 

join() is going to wait forever.

detatch() doesn't kill thread either. Actually it tells std::thread that this thread should continue to run even when std::thread object is destroyed. C++ checks in std::thread destructor that thread is either joined or detached and terminates program if this check fails.

So if you uncomment first line in main function of the following code it will crash. If you uncomment second or third line it will work ok.

#include <thread>  void func() { }  void fail1() {     std::thread t(func);     // will fail when we try to destroy t since it is not joined or detached }  void works1() {     std::thread t(func);     t.join(); }  void works2() {     std::thread t(func);     t.detach(); }  int main() {     // fail1();     // works1();     // works2(); } 
like image 41
Dmitry Ermolov Avatar answered Sep 22 '22 08:09

Dmitry Ermolov