Launch policies
Apart from specifying the function or callable object as an argument when using the std::async
function, we can also specify the launch policy. Launch policies control how std::async
schedules the execution of asynchronous task. These are defined in the <
future>
library.
The launch policy must be specified as the first argument when calling std::async
. This argument is of the type std::launch
, a bitmask value where its bits control the allowed methods of execution, which can be one or more of the following enumeration constants:
std::launch::async
: The task is executed in a separate thread.std::launch::deferred
: Enables lazy evaluation by executing the task in the calling thread the first time its result is requested via the futureget()
orwait()
method. All further accesses to the samestd::future
will return the result immediately. That means that the task will only be executed when the result is explicitly requested, which can lead to unexpected...