《C++ Concurrency in Action》笔记2 线程函数传参(4)move

来源:互联网 发布:编写linux脚本执行命令 编辑:程序博客网 时间:2024/06/05 08:00
有时,要给线程函数传递的参数是不可拷贝的,比如: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;}
输出如下:
123456请按任意键继续. . .

阅读全文
0 0
原创粉丝点击