gpt4 book ai didi

c++ - move std::thread

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:09:49 28 4
gpt4 key购买 nike

尝试制作简单的代码片段:

std::thread threadFoo;

std::thread&& threadBar = std::thread(threadFunction);

threadFoo = threadBar; // thread& operator=( thread&& other ); expected to be called

出现错误:

use of deleted function 'std::thread& std::thread::operator=(const std::thread&)'

我将threadBar 明确定义为右值引用,而不是普通引用。为什么不调用预期的运算符(operator)?如何将一个线程 move 到另一个线程?

谢谢!

最佳答案

命名 引用是左值。左值不绑定(bind)到右值引用。您需要使用 std::move

threadFoo = std::move(threadBar);

关于c++ - move std::thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17674156/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com