async

::std::async can be used to launch a function asynchronously. The return value of the function is stored in a future object, which is returned by ::std::async .

::std::async accepts an optional policy argument which, if given, should be the first argument. Allowed values include ::std::launch::async and std::launch::deferred . The former launches the function on a different thread, which is created and managed by ::std::async. The latter lazily evaluates the function the first time its return value is evaluated using the future.

#include <iostream>
#include <future>

int do_work(void)
{
   return 42;
}

int main(int argc, char ** argv)
{
   auto
      future = ::std::async( std::launch::async, do_work );
   
   future.wait();
   
   ::std::cout << future.get() << ::std::endl;
   
   return 0;
}
Output:
42
#include <iostream>
#include <future>

int do_work(void)
{
   return 42;
}

int main(int argc, char ** argv)
{
   auto
      future =
         ::std::async( std::launch::deferred, do_work );
   
   ::std::cout << future.get() // do_work is evaluated here
               << ::std::endl
                  ;
   
   return 0;
}