Libreria thread C ++ - Funzione get_id

Descrizione

Restituisce l'id del thread.

Dichiarazione

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

id get_id() const noexcept;

C ++ 11

id get_id() const noexcept;

Parametri

nessuna

Valore di ritorno

Restituisce l'id del thread.

Eccezioni

No-throw guarantee - non genera mai eccezioni.

Gare di dati

Si accede all'oggetto.

Esempio

Nell'esempio seguente per std :: thread :: get_id.

#include <iostream>
#include <thread>
#include <chrono>

void foo() {
   std::this_thread::sleep_for(std::chrono::seconds(1));
}

int main() {
   std::thread sample(foo);
   std::thread::id sample_id = sample.get_id();

   std::thread sample2(foo);
   std::thread::id sample2_id = sample2.get_id();

   std::cout << "sample's id: " << sample_id << '\n';
   std::cout << "sample2's id: " << sample2_id << '\n';

   sample.join();
   sample2.join();
}

L'output dovrebbe essere così -

sample's id: 139887397730048
sample2's id: 139887389337344