C ++ Thread Library - Funzione di scollegamento

Descrizione

Ritorna quando l'esecuzione del thread è stata completata.

Dichiarazione

Di seguito è riportata la dichiarazione per la funzione std :: thread :: detach.

void join();

C ++ 11

void join();

Parametri

nessuna

Valore di ritorno

nessuna

Eccezioni

No-throw guarantee - non genera mai eccezioni.

Gare di dati

Si accede all'oggetto.

Esempio

Nell'esempio seguente per 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));
}

L'output dovrebbe essere così -

Starting thread caller.
Starting thread.
Exiting thread caller.
Exiting previous thread.