liveBook · Manning

Move

As explained in C++ Concurrency in Action, Second Edition

Move semantics are C++ language features that allow resources to be transferred from one object to another without copying them.

Move semantics transfer resource ownership rather than copying it. With std::thread, the moved-from object has no associated thread; moving from a temporary is implicit, while moving from a named object requires std::move. section 2.3

void some_function();
void some_other_function();
std::thread t1(some_function);
std::thread t2=std::move(t1);
t1=std::thread(some_other_function);
std::thread t3;
t3=std::move(t2);
t1=std::move(t3);          #1

Follow ownership as it moves among t1, t2, and t3. The final assignment targets a thread-owning t1, so it calls std::terminate(). section 2.3

How C++ Concurrency in Action, Second Edition covers it

How deep it goes
Explains explicit moves from named objects, implicit moves from temporaries, and termination when assignment overwrites a thread-owning destination.
Examples
Uses ownership transfers among three std::thread objects and shows returning or passing threads.
What you can do after
Apply it via std::move to transfer named std::thread ownership without assigning over an already-associated destination.
How it connects
Connects std::thread transfers to move-only standard-library resources and thread-owning wrappers.

Also explained in

Compare how 5 books explain move →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C++ Concurrency in Action, Second Edition. The section links point to the parts of the book it draws on.