std :: thread.join()做什么? [英] What does std::thread.join() do?

查看:319
本文介绍了std :: thread.join()做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 C ++参考的定义


阻塞当前线程,直到 * this 标识的线程完成执行。

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

这意味着当使用 .join()时,不需要 mutex.lock )当线程调用一些函数?我是一个新的互斥和线程,所以我很困惑。

So does this mean when using .join(), there's no need to mutex.lock() when that thread calls some function? I'm new to mutual exclusion and threading, so I'm kind of confused.

注意:我发现一本书
C ++并发在行动和我正在读这本书。它非常适合初学者如我的多线程。

Note: I've found a book C++ Concurrency in Action and I am reading the book. It is very well written for a beginner on multithreading like me.

感谢大家的帮助。

推荐答案

互斥体和条件。加入线程使得一个执行线程等待另一个线程完成运行。您仍然需要互斥体来保护共享资源。它允许main()在这个例子中等待所有线程在退出之前完成。

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

请注意,输出可能如下所示:

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.

因为std :: cout是一个共享资源访问,它的使用也应该是互斥保护。

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

这篇关于std :: thread.join()做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆