Ownership transfer moves responsibility for a resource between objects without copying it. In C++, movable, noncopyable types such as `std::thread` and `std::unique_lock` use this pattern. Moving from a named object requires `std::move`; a temporary can be moved from implicitly. section 2.3 section 3.2.7
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
The sequence moves thread ownership among three objects. After a move, the source no longer owns that thread; assigning into an object that already owns a thread calls `std::terminate()`. section 2.3
A function can return a `std::unique_lock` to let its caller continue working under the same lock. Returning the local lock transfers ownership automatically; the caller can then rely on the protected data remaining unchanged by other threads while the lock is held. section 3.2.7
std::unique_lock<std::mutex> get_lock()
{
extern std::mutex some_mutex;
std::unique_lock<std::mutex> lk(some_mutex);
prepare_data();
return lk; #1
}
void process_data()
{
std::unique_lock<std::mutex> lk(get_lock()); #2
do_something();
}
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":5,\"ch\":52},{\"line\":5,\"ch\":54}],[{\"line\":9,\"ch\":52},{\"line\":9,\"ch\":54}]]"}
!@%STYLE%@!
Here, `get_lock()` acquires the mutex and prepares data before returning its lock. `process_data()` receives the lock and performs further work while retaining protection. section 3.2.7


