Is it safe to re-assign detached boost::thread

Viewed 78

For example

boost::thread th = boost::thread(a_lengthy_function);
th.detach();
th = boost::thread(another_function);

Will first thread be canceled or affected by second thread?

2 Answers

from boost doc:

A thread can be detached by explicitly invoking the detach() member function on the boost::thread object. In this case, the boost::thread object ceases to represent the now-detached thread, and instead represents Not-a-Thread.

thread() noexcept;Effects:Constructs a boost::thread instance that refers to Not-a-Thread

so a detached thread is the same thing as a default constructed thread. So, yes, you can safely move-assign to a default constructed thread, and the answer to your second question is no, the two threads are totally unrelated.

FYI, the same applies to std::thread as well ...

Related