简体   繁体   English

您可以在另一个线程中运行一个线程吗?

[英]Can you run a thread in another thread?

In my programm, I want to run two seperate thread, which are initalized and launched in the main thread. 在我的程序中,我想运行两个单独的线程,这些线程是初始化的并在主线程中启动。 This concept works fine for me, but when I now have to make a cooldown on one of the child threads while it is not paused it seems to still pause the thread; 这个概念对我来说很好用,但是当我现在不得不在不暂停子线程之一的情况下进行冷却时,它似乎仍在暂停线程。

Think of this as an example: 以此为例:

#include <iostream>
#include <thread>

class testClass {
    static void grandchild();
    static void child();
public:
    void parent();
};

void testClass::grandchild() {
    //infinite loop of nothing;
    while (true);
}

void testClass::child() {
    //thread initalising and 
    std::thread grand(&testClass::grandchild);
    std::cout << "before join" << std::endl;
    grand.join();
    std::cout << "after join" << std::endl;
}

void testClass::parent() {
    std::thread child(&testClass::child);
    child.join();
}
int main() {
    testClass parentclass;
    parentclass.parent();
}

The output of this program now is 该程序的输出现在是

http://puu.sh/ppTkR/e29221e1b8.png

This also happens when I do this_thread::sleep_for and other functions which pause a thread. 当我执行this_thread :: sleep_for和其他暂停线程的函数时,也会发生这种情况。 Can anyone explain me why this happens and what is possible for me to implement this properly or if this is even possible in C++? 谁能解释我为什么会发生这种情况,我有什么可能适当地实现它,或者甚至在C ++中有可能实现?

.join() means "wait until that thread finishes". .join()表示“等待该线程完成”。 parent waits for child and child waits for grandchild and grandchild is stuck in an infinite loop, therefore nothing happens. parent等待childchild等待grandchildgrandchild陷入无限循环,因此什么也没发生。 You are effectively running single-threaded because you join with the threads you spawn. 您可以有效地运行单线程,因为您可以与生成的线程连接。

You could not wait by using .detach() instead of .join() , but you should join all the threads before returning from main . 您不能通过使用.detach()而不是.join() .detach()来等待,但是您应该先连接所有线程,然后再从main返回。

This program is behaving correctly. 该程序的行为正确。

  1. It displays "before join". 它显示“加入之前”。

  2. It then waits until the infinite while(1) loop finishes. 然后等待直到无限while(1)循环结束。

  3. After an infinite time, it displays "after join". 无限时间后,它将显示“加入后”。

If you intend different behaviour, perhaps you could explain what that different behaviour is. 如果您打算采取不同的行为,也许您可​​以解释一下这种不同的行为是什么。

Here is a modified program that uses the grandchild thread to wait for 3 seconds before changing a value. 这是一个经过修改的程序,该程序使用孙线程在更改值之前等待3秒。

The child thread keeps running. 子线程继续运行。

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

class testClass {
public:
    void grandchild();
    void child();
    void parent();
    std::atomic<int> value{0};
};

void testClass::grandchild()
{
    std::this_thread::sleep_for(std::chrono::seconds(3));
    value = 5;
}

void testClass::child()
{
    //thread initalising and 
    std::cout << "Starting grandchild thread.\n";
    std::thread grand(&testClass::grandchild,this);
    //grand.detach(); // could detach so that a join() will not be required later
    for (int x{0}; x<5; ++x) {
        std::cout << x << '\t' << value << '\n';
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    grand.join();
}

void testClass::parent()
{
    std::cout << "Starting child thread.\n";
    std::thread child(&testClass::child, this);
    child.join();
}
int main()
{
    std::cout << "Starting program\n";
    std::cout << "Starting parent thread.\n";
    testClass parentclass;
    parentclass.parent();
}

output: 输出:

Starting program
Starting parent thread.
Starting child thread.
Starting grandchild thread.
0       0
1       0
2       0
3       5
4       5

live demo 现场演示

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

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