pretty

Thursday 11 June 2015

C++11 async launch::async vs launch::deferred


With launch::async flag the foo() function would be executed in a separate spawned thread immediately.
On the other hand with launch::deferred foo() will be executed on the same thread, and at the moment when future.wait() or future.get() would be called.
void  foo () {
    std::cout << "foo threadid = " << std::this_thread::get_id() << '\n';
}

int main()
{
    std::cout << "my threadid = " << std::this_thread::get_id() << '\n';
    std::future fut = std::async(std::launch::deferred, foo);
    fut.get();
    return 0;
}
my threadid =  140737353906048
foo threadid = 140737353906048

No comments :

Post a Comment