简体   繁体   English

std :: thread-如何使主线程保持运行并且子分支断开

[英]std::thread - How to make main thread keep running and children branch off

I'm working on a project where I want to create a thread that branches off the main thread, but does not block it until returned. 我正在开发一个项目,在该项目中我想创建一个分支该主线程的线程,但是直到返回时才阻塞它。 What I mean by this is that I need the main thread to create the child thread, and the main thread keep working while the child thread goes its separate way (branching off). 我的意思是,我需要主线程来创建子线程,并且当子线程采用其单独的方式(分支)时,主线程保持工作状态。

I've recreated a smaller-scale version of my code, for simplicity's sake: 为了简单起见,我重新创建了较小规模的代码:

#include <iostream>
#include <thread>

using namespace std;

void wait() {
    std::this_thread::sleep_for(std::chrono::duration<int, std::ratio<1,1000>>(200));
}

void one() {
    static int x = 0;
    while(true) {
        cout << "one" << endl;
        wait();
    }
}

void two() {
    while(true) {
        cout << "two" << endl;
        wait();
    }
}

int main() {
    thread t1(one);
    thread t2(two);

    t1.join();
    t2.join();

    cout << "Do stuff beyond here..";

    while(true) {
        cout << "three" << endl;
        wait();
    }

    return 0;
}

My problem is that the threes don't start showing up until after the ones and twos have finished.. Is there any way around this? 我的问题是,三位一体直到三位一体完成后才开始出现。这有什么办法吗? I've tried omitting the calls to join , but that only crashes the program. 我试过省略join的调用,但这只会使程序崩溃。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Simplest solution 最简单的解决方案

The first idea that come to my mind would be to postpone the joins: 我想到的第一个想法是推迟联接:

atomic<bool> finished=false; 

thread t1(one, &finished);  // signature could be changed to inform when finished
thread t2(two, &finished);

// do your processing here, 
while (! finished) 
    // ......

t1.join();
t2.join();

The only restriction is that the tree access must be synchronized, ie the main thread shall be aware that things are going on in other threads, and shall avoid race conditions. 唯一的限制是必须对树访问进行同步,即,主线程应知道其他线程中正在发生事情,并且应避免争用条件。 It shall also be informed when the other threads are finished, for example, using a shared atomic. 还应告知其他线程何时完成,例如使用共享原子。

Alternative 另类

You could also detach the other threads: 您还可以分离其他线程:

t1.detach(); 
t2.detach(); 

From http://www.cplusplus.com/reference/thread/thread/join/ 来自http://www.cplusplus.com/reference/thread/thread/join/

std::thread::join std :: thread :: join

The function returns when the thread execution has completed. 线程执行完成后,该函数返回。

This synchronizes the moment this function returns with the completion of all the operations in the thread: This blocks the execution of the thread that calls this function until the function called on construction returns (if it hasn't yet). 这将使该函数返回的时刻与线程中所有操作的完成同步: 阻塞调用该函数的线程的执行,直到构造函数上调用的函数返回 (如果尚未返回 )。

When you call join , the main thread stops execution and waits for the child thread to complete. 当您调用join ,主线程停止执行并等待子线程完成。

Try moving the join s to the bottom of main(). 尝试将join s到的主要底部()。


Also RE: 也RE:

I've tried omitting the calls to join, but that only crashes the program. 我试过省略加入的调用,但这只会使程序崩溃。

That behavior is explained at https://stackoverflow.com/a/13984169/3294441 该行为在https://stackoverflow.com/a/13984169/3294441中进行了解释

calling a thread destructor without first calling join (to wait for it to finish) or detach is guarenteed to immediately call std::terminate and end the program 无需先调用join(等待其完成)或detach即可调用线程析构函数被保证立即调用std :: terminate并结束程序

The join is required before the thread falls out of scope. 在线程超出范围之前,需要进行join Simply allow the threads to be joined as late as possible. 只需让线程尽可能晚地加入即可。

In this case after the loop, just before the main function returns. 在这种情况下,循环之后,就在主函数返回之前。

If your functions will exit at some unknown point, you could use thread::detach , but that brings with it its own catches. 如果您的函数将在某个未知点退出,则可以使用thread::detach ,但这会带来它自己的捕获。

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

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