Is std::async guaranteed to be called for functions returning void?
Asked Answered
S

1

7

I've wrote the following code to test std::async() on functions returning void with GCC 4.8.2 on Ubuntu.

#include <future>
#include <iostream>

void functionTBC()
{
    std::cerr << "Print here\n";
}

int main(void)
{
#ifdef USE_ASYNC
    auto i = std::async(std::launch::async, functionTBC);
#else
    auto i = std::async(std::launch::deferred, functionTBC);
#endif
    //i.get();
    return 0;
}

If i.get(); is uncommented, the message "Print here" always exists; however, if i.get(); is commented out, "Print here" exists if and only if USE_ASYNC is defined (that is, std::launch::async always leads to message printed out while std::launch::deferred never).

Is this guaranteed behavior? What's the correct way to ensure the asynchronous call returning void to be executed?

Skiba answered 29/5, 2015 at 16:52 Comment(1)
If you ask for a deferred launch and never call .get on that future, your function will never be executed. This has nothing to do with the function returning void or any other type.Ballet
I
9

std::launch::deferred means "do not run this until I .wait() or .get()".

As you never .get() or .wait()ed, it never ran.

void has nothing to do with this.

For std::launch::async, the standard states that the returned future's destructor (~future) will block until the task is complete (ie, has an implicit .wait()). This is violated by MSVC on purpose, because they disagreed with that design decision, and they are fighting to change the standard: in practice, this means that you cannot rely on any behavior at all from std::launch::async returned future if you want to future-proof your code.

Without implicit wait in ~future, it would be indeterminate if it actually invoked the function when main exited. It would either have happened, or not. Possibly you could invoke UB by having still-active threads at the end of main.

You may wonder what use deferred has: you can use it to queue up a computation for lazy evaluation.

Insulator answered 29/5, 2015 at 16:57 Comment(7)
If I just drop the return value of std::async() (remove the auto i = in my snippet), the std::launch::deferred version is still skipped. Doesn't this violate "~future will block until the task completes"?Skiba
"~future will block until the task completes" doesn't apply in this case, because of std::launch::deferred. Suggest you pick up a copy of Meyer's "Effective Modern C++", see Item 38.Gretchen
@Skiba The note about ~future blocking only applies when you std::async( std::launch::async. Note that calling std::async( std::launch::async without storing the return value causes it to be destroyed at the end of the statement, so the main thread blocks until the async task is complete on that very line. Storing it in auto i= will make the block occur at the end of the scope. Moving the returned future to another future will defer the block until the future that stores the state is finally destroyed.Insulator
@Yakk Sorry, I accidentally missed the condition "For std::launch::async".Skiba
MSVC 2015's async now returns blocking futures, after the proposed change failed to pass in full committee.Talaria
@Talaria Did they modify the page? I can't find either async or promise word in the link.Halford
@Halford web.archive.org/web/20160112225646/http://blogs.msdn.com/b/…Talaria

© 2022 - 2024 — McMap. All rights reserved.