While reading about multithreading in C++11, I noticed that some tutorials do this:
std::thread(print_message, "Hello").detach();
// instead of...
std::thread t(print_message, "Hello");
t.detach();
My questions are:
std::thread
?std::thread
behaves as any other type.To elaborate more on the second question, the temporary std::thread
object behaves like any other temporary object:
It is destroyed after the full expression it is bound to is evaluated, which means the destructor is always called after the .detach()
call - std::terminate()
is not called.
In answer to question 1: You are not allowed to modify anonymous temporaries of "non-class" type (i.e.. built-in types). You are, however, allowed to call non-const member functions on anonymous temporaries of "class" type. See Section 3.10.10 in C++ ISO/IEC 14882:1998 standard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With