有时,要给线程函数传递的参数是不可拷贝的,比如:std::unique_ptr、std::ifstream等,但是它们可以被move。看下面的程序:
void f(unique_ptr<string> u)
{
cout << *u << endl;
cout << (*u = "456") << endl;
}
void call_by_main()
{
unique_ptr<string> p(new string("123"));
thread t(f, std::move(p));
t.detach();
}
int main()
{
call_by_main();
system("pause");
return 0;
}
输出如下:
123
456
请按任意键继续. . .