A. Sarid A. Sarid - 1 year ago 131
C++ Question

std::thread move constructor

I recently looked at this std::thread reference.

For the move constructor it says:

thread( thread&& other );

Move constructor. Constructs the thread object to represent the thread of execution that was represented by
other. After this call other no longer represents a thread of

Also, in the example below there are these lines:

int n=0;
std::thread t3(f2, n);
std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread

What I don't understand is what exactly happens with thread
waits until
finish its execution? What does it mean that
is no longer a thread?

Answer Source

std::thread isn't a thread. It is a representation of a thread provided by an underlying operating system that you can use to manipulate the thread. This is just like a car object isn't really a car.

move moves the thread being represented from one std::thread to another. After the move, t3 is a gutless std::thread. The std::thread object is still there, but t3 doesn't reference any actual system thread. t4 now represents the thread previously represented by t3, but it will not wait unless you call join.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download