web-dev-qa-db-fra.com

Que fait std :: thread.join ()?

Par définition à partir de la référence C++ :

Bloque le thread actuel jusqu'à ce que le thread identifié par *this Termine son exécution.

Donc, cela signifie-t-il que lorsque vous utilisez .join(), il n'est pas nécessaire de mutex.lock() lorsque ce thread appelle une fonction? Je suis nouveau dans l'exclusion mutuelle et le filetage, donc je suis un peu confus.

Remarque: J'ai trouvé un livre C++ Concurrency in Action et je lis le livre. Il est très bien écrit pour un débutant en multithreading comme moi.

Merci à tous pour l'aide.

30
Guagua

Vous avez encore besoin de mutex et de conditions. Rejoindre un thread fait qu'un thread d'exécution attend qu'un autre thread se termine. Vous avez toujours besoin de mutex pour protéger les ressources partagées. Il permet à main () dans cet exemple d'attendre la fin de tous les threads avant de se quitter.

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

using namespace std;



int global_counter = 0;
std::mutex counter_mutex;

void five_thread_fn(){
    for(int i = 0; i<5; i++){
        counter_mutex.lock();
        global_counter++;
        counter_mutex.unlock();
        std::cout << "Updated from five_thread"  << endl;
        std::this_thread::sleep_for(std::chrono::seconds(5));
    }
    //When this thread finishes we wait for it to join
}

void ten_thread_fn(){
    for(int i = 0; i<10; i++){
        counter_mutex.lock();
        global_counter++;
        counter_mutex.unlock();
        std::cout << "Updated from ten_thread"  << endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    //When this thread finishes we wait for it to join
}
int main(int argc, char *argv[]) {
    std::cout << "starting thread ten..." << std::endl;
    std::thread ten_thread(ten_thread_fn);

    std::cout << "Running ten thread" << endl;
    std::thread five_thread(five_thread_fn);


    ten_thread.join();
    std::cout << "Ten Thread is done." << std::endl;
    five_thread.join();
    std::cout << "Five Thread is done." << std::endl;
}

Notez que la sortie peut ressembler à ceci:

starting thread ten...
Running ten thread
Updated frUopmd atteend_ tfhrroema df
ive_thread
Updated from ten_thread
Updated from ten_thread
Updated from ten_thread
Updated from ten_thread
Updated from five_thread
Updated from ten_thread
Updated from ten_thread
Updated from ten_thread
Updated from ten_thread
Updated from ten_thread
Updated from five_thread
Ten Thread is done.
Updated from five_thread
Updated from five_thread
Five Thread is done.

Étant donné que std :: cout est un accès aux ressources partagées et son utilisation doit également être protégée par mutex.

22
Joel

join () arrête le thread en cours jusqu'à ce qu'un autre se termine. mutex arrête le thread actuel jusqu'à ce que le propriétaire du mutex le libère ou le verrouille immédiatement s'il n'est pas verrouillé. Donc ces gars sont assez différents

7
event

Il bloque le thread actuel jusqu'à la fin de l'exécution du thread sur lequel join () est appelé.

Si vous ne spécifiez pas join () ou dettach () sur le thread, cela entraînera une erreur d'exécution car le thread principal/actuel terminera son exécution et l'autre thread créé sera toujours en cours d'exécution.

3
Monish