DigestCPP

Lets Understand With Example

  • Home
  • Design Principal
  • Design Patterns
  • C++ 11 Features
  • C++11 Multithreading
  • Contact Us

std::async with std::launch c++11

std::async

It is an API provided by c++ standard library to execute the task (that is passed as argument) either asynchronously (create separate thread)  or synchronously (normal call to function). It depends upon launch policy.

Launch policy is part of std::async API and it is passed as first argument. There are three launch policies:

  1. std::launch::async
  2. std::launch::deferred
  3. Default Policy (std::launch::async | std::launch::deferred)

std::launch::async

It starts the function as separate thread, if resource is unavailable then we will get exception (std::system_error with error std::resource::unavailable_try_again). Example:

//Using std::launch::async policy, create separate thread
std::future<int>result1=std::async(std::launch::async,AddThread,3,3);

std::launch::deferred

It starts the function as synchronous call when get() or wait() of future class is called. If someone launch async with deferred policy and don’t call the get() or wait() of future class then function will never get started. Example:

//Using std::launch::deferred policy, no separate thread.
auto result2 = std::async(std::launch::deferred, MulThread, 3, 3);

Default Policy

It is combination of both above policies or leave empty (please check below example), it starts the function  either asynchronously (create separate thread)  or synchronously (normal call to function), it depends upon library, library will select one of above policy based on availability of resources. Example:

//Using Default Policy
//Both below function signature are same, we can use anyone.
std::async(std::launch::async|std::launch::deferred, AddThread, 6, 6);
or
std::async(AddThread, 6, 6);

Source code with example:

/*
Program: async with Launch policies
Author: Alpha Master
Date:30 Jan 2021
*/

//Header File
#include<iostream>
#include<future>
#include<exception>

//Thread1
int AddThread(int a, int b)
{
    std::cout<<"Add Thread"<<std::endl;
    return a+b;
}

//Thread2
int MulThread(int a, int b)
{
    std::cout<<"Mul Thread"<<std::endl;
    return a*b;
}



int main()
{
    std::cout<<"Main thread to display the data"<<std::endl;
  
    //Using std::launch::async policy, create separate thread
    std::future<int>result1 = std::async(std::launch::async, AddThread, 3, 3);

    //Using std::launch::deferred policy, no separate thread(synchronous call).
    //MulThread will be called when get() is called and its blocking call.
    auto result2 = std::async(std::launch::deferred, MulThread, 3, 3);

    //Using async Default policy, it depends upon library.
    //std::async(std::launch::async|std::launch::deferred, AddThread, 6, 6);
    std::future<int>result3 = std::async(AddThread, 6, 6);

    //get is blocking call
    std::cout<<"Add result:"<<result1.get()<<std::endl;
    std::cout<<"Mul Result:"<<result2.get()<<std::endl;
    std::cout<<"Default POlicy Add result:"<<result3.get()<<std::endl;

    return 0;
}

Output:

Main thread to display the data
Add Thread
Add Thread
Add result:6
Mul Thread
Mul Result:9
Default POlicy Add result:12

Primary Sidebar




DigestCPP © 2023. All rights reserved.

    About Privacy Policy Terms and Conditions Contact Us Disclaimer