Bibliothèque de threads C ++ - Jointure de fonction

La description

Il retourne lorsque l'exécution du thread est terminée.

Déclaration

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

void join();

C ++ 11

void join();

Paramètres

aucun

Valeur de retour

aucun

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 :: join.

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

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

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

int main() {
   std::cout << "starting helper...\n";
   std::thread helper1(foo);

   std::cout << "starting another helper...\n";
   std::thread helper2(bar);

   std::cout << "waiting for helpers to finish..." << std::endl;
   helper1.join();
   helper2.join();

   std::cout << "done!\n";
}

La sortie devrait être comme ça -

starting helper...
starting another helper...
waiting for helpers to finish...
done!