目录

detach

描述 (Description)

它在线程执行完成时返回。

声明 (Declaration)

以下是std :: thread :: detach函数的声明。

void join();

C++11

void join();

参数 (Parameters)

没有

返回值 (Return Value)

没有

异常 (Exceptions)

No-throw guarantee - 永不抛出异常。

数据竞争 (Data races)

访问该对象。

例子 (Example)

在下面的示例中为std :: thread :: detach。

#include <iostream>
#include <chrono>
#include <thread>
void independentThread() {
   std::cout << "Starting thread.\n";
   std::this_thread::sleep_for(std::chrono::seconds(2));
   std::cout << "Exiting previous thread.\n";
}
void threadCaller() {
   std::cout << "Starting thread caller.\n";
   std::thread t(independentThread);
   t.detach();
   std::this_thread::sleep_for(std::chrono::seconds(1));
   std::cout << "Exiting thread caller.\n";
}
int main() {
   threadCaller();
   std::this_thread::sleep_for(std::chrono::seconds(5));
}

输出应该是这样的 -

Starting thread caller.
Starting thread.
Exiting thread caller.
Exiting previous thread.
↑回到顶部↑
WIKI教程 @2018