The 2011 standard adds std::move to the <utility> header. Using this template method, one can move resources between objects. This means that it can also move thread instances:
#include <thread>
#include <string>
#include <utility>
void worker(int n, string t) {
// Business logic.
}
int main () {
std::string s = "Test";
std::thread t0(worker, 1, s);
std::thread t1(std::move(t0));
t1.join();
return 0;
}
In this version of the code, we create a thread before moving it to another thread. Thread 0 thus ceases to exist (since it instantly finishes), and the execution of the thread function resumes in the new thread that we create.
As a result of this, we do not have to wait for the first thread to re join, but only for the second one.