2013-03-01 63 views
21

通过definition from C++ referencestd :: thread.join()是做什么的?

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

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

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

谢谢大家的帮助。

+3

谢谢大卫的编辑使其更加清晰。 马丁:我试图谷歌,但我看到4-5样本有相同的想法,并没有真正显示从我的角度来看,联合正在做什么..我认为这里的人工智能是知识渊博,并会提供代码的最佳答案样本。 – Guagua 2013-03-01 03:18:15

回答

17

你仍然需要互斥体和条件。加入一个线程会使一个执行线程等待另一个线程完成运行。您仍然需要互斥体来保护共享资源。它允许这个例子中的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. 

由于性病::法院是它的一个共享资源的访问和使用也应互斥保护了。

+1

谢谢Joel,那么这里的ten_thread.join()会告诉程序ten_thread已经完成,并且可以进入下一步? – Guagua 2013-07-18 18:16:04

+1

运行main()的执行线程将在调用ten_thread.join()时阻塞,直到ten_thread返回。一旦ten_thread执行结束,main的执行就被允许继续。希望这可以帮助。 – Joel 2013-07-18 20:24:57

6

join()会停止当前线程直到另一个线程完成。互斥锁会停止当前线程,直到互斥锁拥有者释放它或在未锁定时立即锁定。所以这些人是完全不同的

+6

虽然我更喜欢“暂停”而不是“停止”。 – Sebastian 2013-03-01 00:15:55

0

的std ::的Thread.join有三个功能,我能想到的副手和其他一些人:

一)鼓励不断创建/终端/销毁线程的,所以锤击性能并增加了泄漏的probabilty,线程失控,内存失控以及应用程序的一般失控。

b)通过强制执行不需要的等待来填充GUI事件处理程序,导致您的客户不愿意回应的'沙漏应用程序'。

c)导致应用程序无法关闭,因为它们正在等待终止不可中断的不可中断线程。 d)其他不好的事情。

我知道你是多线程的新手,我希望你能用它做到最好。另外,考虑到我今晚有很多Adnams Broadside,但是:

Join()和它的其他语言中的朋友,如TThread.WaitFor,(Delphi),都是为了像Windows ME那样的高效多线程操作系统。

请尽力去发展并理解其他多线程概念 - 池,任务,应用程序生存期线程,通过生产者 - 消费者队列的线程间通信。实际上,几乎除Join()之外的任何东西。

+3

您好马丁,任何推荐阅读关于多线程在c + +? – Guagua 2013-03-01 03:20:45

1

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

如果你没有在线程上指定join()或dettach(),那么当主/当前线程将完成其执行并且其他创建的线程仍将运行时,它将导致运行时错误。

相关问题