小编rud*_*dky的帖子

为什么我的线程不能在后台运行?

在上市波纹管,我希望当我打电话t.detach()时创建线程行之后,该线程t将在后台运行,而printf("quit the main function now \n")会叫,然后main将退出.

#include <thread>
#include <iostream>

void hello3(int* i)
{

    for (int j = 0; j < 100; j++)
    {
        *i = *i + 1;
        printf("From new thread %d \n", *i);
        fflush(stdout);

    }

    char c = getchar();
 }

int main()
{
    int i;
    i = 0;
    std::thread t(hello3, &i);
    t.detach();
    printf("quit the main function now \n");
    fflush(stdout);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然而,从它在屏幕上打印的内容来看并非如此.它打印

From new thread 1
From new thread …
Run Code Online (Sandbox Code Playgroud)

c++ parallel-processing multithreading c++11

5
推荐指数
1
解决办法
671
查看次数

标签 统计

c++ ×1

c++11 ×1

multithreading ×1

parallel-processing ×1