What does std::thread.join() do?

后端 未结 4 2135
北荒
北荒 2020-12-25 10:53

By definition from C++ reference:

Blocks the current thread until the thread identified by *this finishes its execution.

4条回答
  •  独厮守ぢ
    2020-12-25 11:16

    You still need mutexes and conditions. Joining a thread makes one thread of execution wait for another thread to finish running. You still need mutexes to protect shared resources. It allows main() in this example to wait for all threads to finish before quitting itself.

    #include 
    #include 
    #include 
    #include 
    
    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;
    }
    

    Note that the output might look like this:

    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.
    

    Since std::cout is a shared resource access and use of it should also be mutex protected too.

提交回复
热议问题