Bibliothèque de threads C ++ - Fonction get_id

La description

Il renvoie l'ID du thread.

Déclaration

Voici la déclaration de la fonction std :: thread :: get_id.

id get_id() const noexcept;

C ++ 11

id get_id() const noexcept;

Paramètres

aucun

Valeur de retour

Il renvoie l'ID du thread.

Exceptions

No-throw guarantee - ne jette jamais d'exceptions.

Courses de données

L'objet est accédé.

Exemple

Dans l'exemple ci-dessous pour 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();
}

La sortie devrait être comme ça -

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