jeudi 26 novembre 2015

Why doesn't destructor of a future object created by async with launch::deferred block?

N4527 30.6.4 [futures.state]/p5

5 When an asynchronous return object or an asynchronous provider is said to release its shared state, it means:

(5.1) — if the return object or provider holds the last reference to its shared state, the shared state is destroyed; and

(5.2) — the return object or provider gives up its reference to its shared state; and

(5.3) — these actions will not block for the shared state to become ready, except that it may block if all of the following are true: the shared state was created by a call to std::async, the shared state is not yet ready, and this was the last reference to the shared state.

30.6.6 [futures.unique_future]

~future();

9 Effects:

(9.1) — releases any shared state (30.6.4);

(9.2) — destroys *this.

#include <iostream>
#include <future>
using namespace std;

void foo(){}

int main()
{
    {   
        auto f1(async(launch::deferred,foo));
    }
    cout << "why does not block?" << endl;
}

why does not the destructor of f1 fit this rule?

  1. the shared state was created by a call to std::async which is async(launch::async,foo)

  2. I don't call f1.get() or f1.wait() so the share state is not ready

  3. f1 is the last reference to the shared state

Aucun commentaire:

Enregistrer un commentaire