描述
它在線程執行完成後返回。
聲明
以下是 std::thread::detach 函數的聲明。
void join();
C++11
void join();
參數
空
返回值
空
異常
No-throw guarantee─ 從不拋出異常。
數據競爭
對象被訪問。
示例
在下麵的 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.
相關用法
- C++ Thread get_id()用法及代碼示例
- C++ Thread get_id用法及代碼示例
- C++ Thread joinable用法及代碼示例
- C++ Thread join用法及代碼示例
- C++ Thread hardware_concurrency()用法及代碼示例
- C++ Thread joinable()用法及代碼示例
- C++ Thread constructor用法及代碼示例
- C++ unordered_map cbegin用法及代碼示例
- C++ map lower_bound()用法及代碼示例
- C++ Unordered_multimap reserve()用法及代碼示例
- C++ list assign()用法及代碼示例
- C++ std::max()用法及代碼示例
- C++ std::string::push_back()用法及代碼示例
- C++ Array swap()用法及代碼示例
- C++ valarray cos用法及代碼示例
- C++ multimap key_comp()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Thread Library - Function detach。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。