Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the behavior of std::async with std::launch::async policy

I have some question about behavior of std::async function with std::launch::async policy & std::future object returned from async.

In following code, main thread waits for the completion of foo() on the thread created by async call.

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

void foo()
{
  std::cout << "foo:begin" << std::endl;
  std::this_thread::sleep_for(std::chrono::seconds(10));
  std::cout << "foo:done" << std::endl;
}

int main()
{
  std::cout << "main:begin" << std::endl;
  {
    auto f = std::async(std::launch::async, foo);
    // dtor f::~f blocks until completion of foo()... why??
  }
  std::this_thread::sleep_for(std::chrono::seconds(2));
  std::cout << "main:done" << std::endl;
}

And I know http://www.stdthread.co.uk/doc/headers/future/async.html says

The destructor of the last future object associated with the asynchronous state of the returned std::future shall block until the future is ready.

My question is:

  • Q1. Does this behavior conform to the current C++ standard?
  • Q2. If Q1's answer is yes, which statements say that?
like image 566
yohjp Avatar asked Mar 16 '12 07:03

yohjp


People also ask

Does STD async start a new thread?

If the deferred flag is set, a callable function will be stored together with its arguments, but the std::async function will not launch a new thread.

What is std :: async?

The function template async runs the function f asynchronously (potentially in a separate thread which might be a part of a thread pool) and returns a std::future that will eventually hold the result of that function call. 1) Behaves as if (2) is called with policy being std::launch::async | std::launch::deferred.

What does async do in C++?

As the name indicates, C++ async is a function template fn, which takes functions or function objects as arguments (basically called callbacks) and runs them asynchronously. It returns the std:: the future object which is used to keep the result of the above function. The result is stored in the shared state.

How do I use async in CPP?

So if you want to make sure that the work is done asynchronously, use std::launch::async . @user2485710 it needs to block when you retrieve the result, if you need the result in the launching thread. It cannot use the result if the result is not ready. So if you go to get the result, you have to wait until it is ready.


1 Answers

Yes, this is required by the C++ Standard. 30.6.8 [futures.async] paragraph 5, final bullet:

— the associated thread completion synchronizes with (1.10) the return from the first function that successfully detects the ready status of the shared state or with the return from the last function that releases the shared state, whichever happens first.

The destructor of the one and only std:future satisfies that condition, and so has to wait for the completion of the thread.

like image 192
Anthony Williams Avatar answered Sep 23 '22 23:09

Anthony Williams