简体   繁体   English

如果返回值不是左值,为什么主线程要等待 std::async() 创建的线程?

[英]Why does the main thread wait the thread created by std::async() if the return value is not hold by lvalue?

When I run the following code,当我运行以下代码时,

#include <thread>
#include <iostream>
#include <future>

int main() {
    auto fut = std::async(
        std::launch::async, 
        []{
            std::this_thread::sleep_for(std::chrono::seconds(1));
            std::cout << "sub : " << std::this_thread::get_id() << std::endl;
        }
    ); 

    std::cout << "do some on main thread" << std::endl;
    
    fut.get();

    std::cout << "main: " << std::this_thread::get_id() << std::endl;
}

I got the following output.我得到以下输出。

do some on main thread
sub : 139899103246080
main: 139899103250240

Running demo: https://godbolt.org/z/c9WedY4oq运行演示: https ://godbolt.org/z/c9WedY4oq

It is the same behavior as I expected.这与我预期的行为相同。 "do some on main thread" outputs first, because the sub thread created by std::async() waits 1 second the beggining of the thread. “在主线程上做一些事情”首先输出,因为std::async()创建的子线程在线程开始时等待 1 秒。

So far, so good.到目前为止,一切都很好。


However, when I removed the variable fut , then I got weird behavior for me.但是,当我删除变量fut时,我的行为很奇怪。 NOTE: This code is for only experimental purpose注意:此代码仅用于实验目的

#include <thread>
#include <iostream>
#include <future>

int main() {
    std::async(
        std::launch::async, 
        []{
            std::this_thread::sleep_for(std::chrono::seconds(1));
            std::cout << "sub : " << std::this_thread::get_id() << std::endl;
        }
    ); 

    std::cout << "do some on main thread" << std::endl;
    
    std::cout << "main: " << std::this_thread::get_id() << std::endl;
}

Here is the output:这是输出:

sub : 139716056966912
do some on main thread
main: 139716056971072

Running demo: https://godbolt.org/z/obzzceGGr运行演示: https ://godbolt.org/z/obzzceGGr

It seems that the main thread waits until finishing the sub thread before outputing "do some on main thread".似乎主线程在输出“在主线程上做一些事情”之前一直等到子线程完成。

I want to know why this behavior happens.我想知道为什么会发生这种行为。

I got the warning message ":6:5: warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]".我收到警告消息“:6:5:警告:忽略使用'nodiscard'属性[-Wunused-result]声明的函数的返回值”。 Since C++20, the nodiscard attribute has been added.从 C++20 开始,添加了 nodiscard 属性。 See https://en.cppreference.com/w/cpp/thread/async请参阅https://en.cppreference.com/w/cpp/thread/async

I guess that I got an undefined behavior due to ignoring the return value of std::async() , but I couldn't find such document, so far.我想由于忽略了std::async()的返回值,我得到了未定义的行为,但到目前为止我找不到这样的文档。

In the second case, a std::future object will still be created and returned.在第二种情况下,仍然会创建并返回一个std::future对象。

That object is ephemeral and will be destructed immediately, and that leads to your problem because the std::future destructor will wait for the future to be ready before destruction continues.该对象是短暂的,将立即被破坏,这会导致您的问题,因为std::future析构函数将等待未来准备就绪,然后再继续破坏。

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

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