简体   繁体   English

父线程join():在子级完成之前阻止?

[英]Parent thread join(): Blocks Until Children Finish?

I have a C++ class that does some multi-threading. 我有一个C ++类,它执行一些多线程处理。 Consider the pseudo-code below: 考虑下面的伪代码:

void MyClass::Open() {
  loop_flag =  true;
  // create consumer_thread (infinite loop)
  // create producer_thread (infinite loop)
}
void MyClass::Close() {
  loop_flag = false;
  // join producer_thread
  // join consumer_thread
}
MyClass::~MyClass() {
  Close();
  // do other stuff here
}

Note that consumer_thread, producer_thread, and their associated functions are all encapsulated in MyClass. 请注意,consumer_thread,producer_thread及其相关函数都封装在MyClass中。 The caller has no clue that their calls are multi-threaded and what's going on in the background. 调用者不知道他们的调用是多线程的,并且后台发生了什么。

Now, the class is part of a larger program. 现在,该类是更大程序的一部分。 The program has some initial multi-threading to handle configuration of the system since there's a ton of stuff happening at once. 该程序具有一些初始的多线程来处理系统的配置,因为一次发生了很多事情。

Like this (pseudo-code): 像这样(伪代码):

int main() {
  // create config_thread1 (unrelated to MyClass)
  // create thread for MyClass::Open()
  // ...
  // join all spawned configuration threads
}

So my question is, when I call join() for the thread linked to MyClass::Open() (ie, the configuration thread spawned in main()), what happens? 所以我的问题是,当我为链接到MyClass :: Open()的线程调用join()时(即在main()中产生的配置线程),会发生什么? Does it join() immediately (since the MyClass::Open() function just returns after creation of producer_thread and consumer_thread) or does it wait for producer_thread and consumer_thread to finish (and therefore hangs my program). 它是立即join()(因为MyClass :: Open()函数在创建producer_thread和consumer_thread之后才返回),还是等待producer_thread和Consumer_thread完成(因此挂起了我的程序)。

Thanks in advance for the help. 先谢谢您的帮助。 In terms of implementation details, I'm using Boost threads on a Linux box. 关于实现细节,我在Linux机器上使用Boost线程。

Edited to add this diagram: 编辑添加此图:

 main()
 |
 |
 |
 |--->configuration_thread (that runs MyClass::Open())
   |
   |
   |----> producer_thread
   |----> consumer_thread

If I call join() on configuration_thread(), does it wait until producer_thread() and consumer_thread() are finished or does it return immediately (and producer_thread() and consumer_thread() continue to run)? 如果我在configuration_thread()上调用join(),它会等到producer_thread()和Consumer_thread()完成还是立即返回(并且producer_thread()和Consumer_thread()继续运行)?

A (non detached) thread will be joignable, even after having returned from the function it was set to run, until it has been joined. 一个(非分离的)线程将是可操纵的,即使从该函数返回设置为运行的线程之后,直到它被加入为止。 Example: 例:

#include <iostream>
#include <thread>
#include <chrono>

using namespace std;

void foo(){
    std::cout << "helper: I'm done\n";
}

 int main(){

    cout << "starting helper...\n";
    thread helper(foo);

    this::thread::sleep_for(std::chrono::seconds(5));

     cout << "helper still joignable?..." << (helper.joignable()?"yes!":"no...:(") << "\n";

    helper.join();
    cout << "helper joined!";

    cout << "helper still joignable?..." << (helper.joignable()?"really?":"not anymore!") << "\n";

    cout << "done!\n";

}

Output: 输出:

starting helper...
helper: I'm done
still joinable?...yes!
helper joined!
still joinable?...not anymore!
done!

As for how much time the join method takes, I don't think this is specified, but surely it doesn't't have to wait for all the other threads to finish, or it would mean that only one thread would be able to join all the others. 至于join方法需要花费多少时间,我认为这没有指定,但是可以肯定它不必等待所有其他线程完成,否则就意味着只有一个线程能够加入所有其他人。

From §30.3.5: 从§30.3.5开始:

void Join(); void Join();

Requires: joinable() is true 要求: joinable()true

Effects: Blocks until the thread represented by *this had completed. 效果:阻塞直到由*this表示的线程完成。

Synchronization: The completion of the thread represented by *this synchronises with the corresponding successful join() return. 同步: *this表示的线程的完成与相应的成功join()返回同步。 [ Note: Operations on *this are not synchronised. [ 注意: *this上的操作未同步。 * -- end note*] *-尾注*]

[...] [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM