Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7490703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:42:58+00:00 2026-05-29T15:42:58+00:00

std::async has an overload which takes a std::launch policy as the first argument. When

  • 0

std::async has an overload which takes a std::launch policy as the first argument. When should I use this overload? what are the different policies available? (I think sync and async are the two options). When should I use the sync policy? How is that different from directly running it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:43:01+00:00Added an answer on May 29, 2026 at 3:43 pm

    Summary from the very helpful article that Jagannath linked, and comments on the possible uses.

    There are 3 launch policies:

    • any: the library chooses whether to spawn a thread a or not
    • async: you explicitly ask for a thread to be spawned
    • deferred: you explicitly ask for a thread not to be spawned

    Therefore, the deferred policy is a way to get deterministic lazy evaluation (also known as call by need). For example, suppose that you have:

    void MyClass::lazy(std::future<int> const& f) {
      if (this->notReady()) { return; }
      if (this->stillNotReady()) { return; }
      if (this->value() == 42) { return; }
    
      this->go(f.get());
    }
    

    Now, if computing the value of this integer is long (for example, it may invoke a network roundtrip), then it’s kinda wasteful to compute it in all the cases that do not really require it… and now we’ve got the tool to do so!

    void func(MyClass& mc) {
      std::future<int> f = std::async(std::launch::deferred, []() {
                             return stoi(memcached.get("KEY"));
                           });
    
      mc.lazy(f);
    }
    

    Note that this is subtly different from using a std::function<int()> (and a closure), because the computation is done once and for all, guaranteeing that subsequent calls to get always return the same result.

    The difference with the other policies can also be expressed in term of whether the operation is performed when you do not need the value.

    • any: might be performed on another thread (proactively) or not performed at all
    • async: will be performed on another thread
    • deferred: will not be performed

    Therefore, deferred gives you better control, which is important if the call has a side effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been thinking about std::async and how one should use it in future compiler
std::auto_ptr is broken in VC++ 8 (which is what we use at work). My
std::string x(x); This crashes very badly on my compiler. Does this mean I should
std::pair has the nested typedefs first_type and second_type which give the type of the
I'm trying to get std::async to launch a function that accepts a unique_ptr with
std::cout << (DWORD)test; If I compile and run this I get different output values
std::next_permutation (and std::prev_permutation) permute all values in the range [first, last) given for a
std::find_if takes a predicate in one of it's overloaded function. Binders make it possible
std::string provides const char* c_str ( ) const which: Get C string equivalent Generates
std::string str; std::stringstream strm(str); I get this error: Error 11 error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.