std::线程移动构造函数

std::thread move constructor

本文关键字:构造函数 移动 线程 std      更新时间:2023-10-16

我最近研究了这个std::线程引用。

对于move构造函数,它说:

thread( thread&& other ); 

移动构造函数。构造线程对象以表示由其他此调用之后,other不再代表执行

此外,在下面的示例中,有以下行:

int n=0;
std::thread t3(f2, n);
std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread

我不明白的是线程t3t4到底发生了什么?t4是否等待直到t3完成执行?t3不再是线程意味着什么?

std::thread不是线程。它是底层操作系统提供的线程的表示,您可以使用它来操作线程。这就像car对象不是真正的汽车一样。

CCD_ 8将所表示的线程从一个CCD_。在move之后,t3是无骨的std::threadstd::thread对象仍然存在,但t3没有引用任何实际的系统线程。t4现在表示以前由t3表示的线程,但它不会等待,除非您调用join