C++ std :: async不会产生新线程

Als*_*ton 1 c++ multithreading asynchronous stdasync

C++ 11

int main(int argc, char** argv) {
    std::async(std::launch::async, [](){ 
        while(true) cout << "async thread" <<endl; 
    });
    while(true) cout << "main thread" << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我期望输出应该是交错的async thread, main thread因为应该有2个不同的线程.

但事实并非如此.

它输出:

async thread
async thread
async thread
async thread
...
Run Code Online (Sandbox Code Playgroud)

我想只有一个主题.有人能告诉我为什么它不会产生新的线程std::async吗?谢谢.

Als*_*ton 5

改为:

auto _ = std::async(std::launch::async, [](){ 
    while(true) cout << "async thread" <<endl; 
});
Run Code Online (Sandbox Code Playgroud)

文献:

如果从std :: async获取的std :: future未从引用移动或绑定到引用,则std :: future的析构函数将在完整表达式结束时阻塞,直到异步操作完成为止,基本上会生成如下代码:以下同步:

std :: async(std :: launch :: async,[] {f();}); //临时的dtor等待f()std :: async(std :: launch :: async,[] {g();}); //在f()完成之前不会启动