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 8839989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:21:45+00:00 2026-06-14T10:21:45+00:00

Using C++11 std::async in this snippet: int foo() { ::sleep(2); return 123; } int

  • 0

Using C++11 std::async in this snippet:

int foo()
{
    ::sleep(2);
    return 123;
}

int main()
{
    future<int> r1(async(foo));
    int r2 = foo();
    cout << r1.get() + r2 << endl;
    return 0;
}

It produces the right result, but runs both foo’s serially (whole app runs 4 seconds). Compiled as:
g++ -std=gnu++11 -O2 foo.cc -lpthread (Ubuntu 12.10 64bit, gcc 4.7.2)

  • 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-06-14T10:21:47+00:00Added an answer on June 14, 2026 at 10:21 am

    You might need to add a launch policy of std::launch::async:

    std::async(std::launch::async, foo);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first attempt at using std::future . I have three different files
I would like to implement a method like this: boost::unique_future<int> foo() { return defer([=]
I am using std::unique_ptr in this piece of code which compiles and runs as
I'm having a hard time using std::string::iterators in C++. This code compiles fine (still
//using namespace std; using std::ifstream; using std::ofstream; using std::cout; class Dog { friend ostream&
I was thinking about this when I ran into a problem using std::ofstream. My
Possible Duplicate: Cost of using std::map with std::string keys vs int keys? if I
C++0x shows an example of using std::forward : template<class T> void foo(T&& arg) {
I am trying to print out the following string using std::cout : Encryptor –pid1
I'm using vc2011 and it turns out the std::async(std::launch::async, ... ) is a bit

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.