cpp: thread join(),detach()

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

Ask Question

Asked 3 years, 7 months ago

Active 3 years, 5 months ago

Viewed 18k times

 

22

 

15

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

c++ multithreading c++11 join detach

shareimprove this question

edited Jul 1 '16 at 6:00

Holt

30k66 gold badges6161 silver badges101101 bronze badges

asked May 3 '16 at 22:59

r.hg

22511 gold badge22 silver badges55 bronze badges

add a comment

2 Answers

activeoldestvotes

35

 

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++ threadobject 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.

shareimprove this answer

edited May 5 '16 at 1:22

 

Charles

1,2791010 silver badges1616 bronze badges

answered May 4 '16 at 7:57

Michael Burr

295k4141 gold badges459459 silver badges693693 bronze badges

add a comment

12

 

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();
}

shareimprove this answer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值