C++ std::thread.join() 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15148057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does std::thread.join() do?
提问by Guagua
By definition from C++ reference:
根据C++ 参考的定义:
Blocks the current thread until the thread identified by
*this
finishes its execution.
阻塞当前线程,直到由 标识的线程
*this
完成其执行。
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.
那么这是否意味着在使用时,该线程调用某个函数时.join()
就不需要了mutex.lock()
?我是互斥和线程的新手,所以我有点困惑。
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.
注意:我找到了一本书 C++ Concurrency in Action 并且正在阅读这本书。对于像我这样的多线程初学者来说,它写得非常好。
Thank you all for the help.
谢谢大家的帮助。
采纳答案by Joel
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.
您仍然需要互斥锁和条件。加入一个线程会使一个执行线程等待另一个线程完成运行。您仍然需要互斥锁来保护共享资源。它允许本例中的 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;
}
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.
由于 std::cout 是共享资源访问,因此它的使用也应该受到互斥保护。
回答by event
join() stops current thread until another one finishes. mutex stops current thread until mutex owner releases it or locks right away if it isn't locked. So these guys are quite different
join() 停止当前线程,直到另一个线程完成。互斥锁停止当前线程,直到互斥锁所有者释放它或如果它没有被锁定立即锁定。所以这些人很不一样
回答by helloworld134
It blocks the current thread until the execution of the thread is completed on which join() is called.
它阻塞当前线程,直到调用 join() 的线程执行完成。
If you do not specify join() or dettach() on the thread then it will result in runtime error as the main/current thread will complete its execution and the other thread created will be still running.
如果您没有在线程上指定 join() 或 dettach() ,那么它将导致运行时错误,因为主/当前线程将完成其执行而创建的另一个线程将仍在运行。
回答by Martin James
std::thread.join has three functions I can think of off-hand and some others:
std::thread.join 有我能想到的三个函数和其他一些函数:
a) Encourages continual creating/terminating/destroying of threads, so hammering performance and increasing the probabilty of leaks, thread-runaway, memory-runaway and general loss-of-control of your app.
a) 鼓励不断创建/终止/销毁线程,从而影响性能并增加应用程序泄漏、线程失控、内存失控和总体失控的可能性。
b) Stuffs GUI event-handlers by enforcing unwanted waits, resulting in unresponsive 'hourglass apps' that your customers will hate.
b) 通过强制执行不必要的等待来填充 GUI 事件处理程序,从而导致您的客户讨厌的无响应的“沙漏应用程序”。
c) Causes apps to fail to shutdown because they are waiting for the termination of an unresposive, uninterruptible thread.
c) 导致应用程序无法关闭,因为它们正在等待无响应、不可中断的线程终止。
d) Other bad things.
d) 其他坏事。
I understand that you are new to multithreading, and I wish you the best with it. Also, consider that I've had a lot of Adnams Broadside tonight, but:
我了解您对多线程不熟悉,我祝您一切顺利。另外,考虑到我今晚有很多 Adnams Broadside,但是:
Join(), and it's friends in other languages like TThread.WaitFor, (Delphi), are to efficient multithreading like Windows ME was to operating systems.
Join() 和它在其他语言中的朋友,如 TThread.WaitFor,(Delphi),是高效的多线程,就像 Windows ME 之于操作系统一样。
Please try hard to progress and come to understand other multithreaded concepts - pools, tasks, app-lifetime threads, inter-thread comms via producer-consumer queues. In fact, almost anything except Join().
请努力进步并理解其他多线程概念 - 池、任务、应用程序生命周期线程、通过生产者-消费者队列的线程间通信。事实上,除了Join() 之外,几乎任何东西。