std::async with temporary future returned

Here is exerpt from en.cppreference.com

If the std::future obtained from std::async has temporary object lifetime (not moved or bound to a variable), the destructor of the std::future will block at the end of the full expression until the asynchronous operation completes, essentially code such as the following synchronous:

std::async(std::launch::async, []{ f(); }); // temporary's dtor waits for f()
std::async(std::launch::async, []{ g(); }); // does not start until f() completes

But when i tried with VC++:

void fasync(char ch)
{
    for (int i = 0; i < 10; ++i)
    {
        this_thread::sleep_for(chrono::milliseconds(100));
        cout << ch << endl;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    async(launch::async, [] { fasync('+'); });
    async(launch::async, [] { fasync('*'); });

    this_thread::sleep_for(chrono::milliseconds(2000));

    return 0;
}

i got something like this

+ * +*

*+

*+

* + *+

+*

+*

+*

+*

Looks like nobody waits for fasync('+')?

First i thought that it is because of sleep and changed fasync to

for (int i = 0; i < 10; ++i)
{
    cout << ch << endl;
}
cin.get();

but output interleaving again.

EDIT: According to the link provided by TC it is a known bug of VS 2013.

链接地址: http://www.djcxy.com/p/30734.html

上一篇: std :: launch :: async像同步过程一样阻塞

下一篇: std :: async临时将来返回