简体   繁体   中英

std::async from std::async in windows xp

This sample prints all 2 messages under windows7 and on ideone.com, but fails to print second message on windows xp. What im doing wrong? If it is a bug, where i should report it?

Compiled for windows xp using visual studio 2017, platform toolset v141_xp.

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

using namespace std;
int main()
{
    auto f1 = async(launch::async, []()->int {
        cout << "in outer async" << endl;

         auto f2 = async(launch::async, []()->int {
             cout << "in inner async" << endl;
             return 2;
         });
         f2.get();

        return 1;
    });
    f1.get();

    return 0;
}

UPD when using std::thread instead of std::async for inner function - it works well on both systems

auto f2 = thread([]()->int {
    cout << "in inner async" << endl;
    return 2;
});
f2.join();

UPD2

visual studio 2017 cl.exe version 19.14.26428 toolset v141_xp

commandline:

/permissive- /Yu"stdafx.h" /GS /GL /analyze- /Wall /Gy /Zc:wchar_t /Zi /Gm- /O2 /sdl /Fd"Release\vc141.pdb" /Zc:inline /fp:precise /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_USING_V110_SDK71_" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Gd /Oy- /Oi /MD /std:c++17 /FC /Fa"Release\" /EHsc /nologo /Fo"Release\" /Fp"Release\testasync.pch" /diagnostics:classic 

UPD3 looks like launch::async is ignored when used on windows xp

vector<future<void>> v;
for( int i = 0; i < 10; i++ )
    v.push_back(async(launch::async, []() {cout << "thread" << endl; this_thread::sleep_for(5s); }));
for( auto &f : v )
    f.get();

on windows7 this tooks ~6seconds to complete, on windows xp ~50 seconds

On Windows std::async(...) sits on top of thread pool. So there can be deadlocks. Inside f1 you run new task and call f2.get() , which blocks until f2 is finished. But if auto f2 = async(...) chose the same thread in which f1 runs, then you have deadlock and your program should not finish. If it does, then this isn't the case.

UPDATE

Please, read about Microsoft implementation of std::async here . It says:

The C++ standard states that if policy is launch::async, the function creates a new thread. However the Microsoft implementation is currently non-conforming. It obtains its threads from the Windows ThreadPool, which in some cases may provide a recycled thread rather than a new one. This means that the launch::async policy is actually implemented as launch::async|launch::deferred

There is another answer , which reveals a particular feature of Microsoft's std::async implementation:

  • It limits the total number of background threads it uses, after which a call to std::async will block until a thread becomes free. On my machine, this number is 768.

So, I assume, if you have only one thread in ThreadPool, then the call to std::async from inside of a task will deadlock. Probably it is your case, taking into account your UPD3 .

I really recommend to read the mentioned answer , so you could understand why Microsoft's std::async is different and how to use it correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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