【问题标题】:What does std::thread.join() do?std::thread.join() 做什么?
【发布时间】:2013-03-01 00:04:03
【问题描述】:

definition from C++ reference:

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

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

注意:我找到了一本书 C++ Concurrency in Action,我正在阅读这本书。对于像我这样的多线程初学者来说,它写得非常好。

感谢大家的帮助。

【问题讨论】:

    标签: c++ multithreading mutex


    【解决方案1】:

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

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

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

    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 是共享资源访问和使用它也应该受到互斥保护。

    【讨论】:

    • 谢谢Joel,所以这里的ten_thread.join() 会告诉程序ten_thread 已经完成,可以进行下一步了吗?
    • 运行 main() 的执行线程将在 ten_thread.join() 的调用处阻塞,直到 ten_thread 返回。一旦ten_thread 执行结束,main 的执行就可以继续进行。希望这会有所帮助。
    • Updated fr[U]o[p]m[d] [at]te[e]n[d]_[ ]t[f]h[r]r[o]e[m]a[ ]d[five_thread]
    【解决方案2】:

    join() 停止当前线程,直到另一个线程完成。互斥锁停止当前线程,直到互斥锁所有者释放它或如果它没有被锁定则立即锁定。所以这些家伙完全不同

    【讨论】:

    • 不过,我更喜欢“暂停”而不是“停止”。
    【解决方案3】:

    它阻塞当前线程,直到调用join()的线程执行完成。

    如果您没有在线程上指定 join() 或 dettach(),则会导致运行时错误,因为主/当前线程将完成其执行,而创建的其他线程仍将运行。

    【讨论】:

      【解决方案4】:

      std::thread.join 有三个我能想到的函数和其他一些函数:

      a) 鼓励持续创建/终止/销毁线程,从而影响性能并增加应用程序发生泄漏、线程失控、内存失控和一般失控的可能性。

      b) 通过强制执行不必要的等待来填充 GUI 事件处理程序,从而导致客户讨厌的无响应的“沙漏应用”。

      c) 导致应用无法关闭,因为它们正在等待一个无响应、不可中断的线程终止。

      d) 其他不好的事情。

      我了解您是多线程的新手,祝您使用它一切顺利。另外,考虑到我今晚有很多 Adnams Broadside,但是:

      Join() 和其他语言的朋友,如 TThread.WaitFor,(Delphi),是高效的多线程,就像 Windows ME 是操作系统。

      请努力进步并理解其他多线程概念 - 池、任务、应用程序生命周期线程、通过生产者-消费者队列的线程间通信。事实上,几乎除了 Join() 之外的任何东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-22
        • 1970-01-01
        • 2014-09-09
        • 1970-01-01
        • 2015-01-30
        相关资源
        最近更新 更多