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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:03:47+00:00 2026-06-12T17:03:47+00:00

In c++11 how would one go about implementing a program that does two expensive

  • 0

In c++11 how would one go about implementing a program that does two expensive (network) calls, of the same type, and then only waits for the result from the quicker one, not waiting and discarding the slower result. std::thread cant be interrupted and does not return a convenient std::future. And std::async that returns a future, can neither be interrupted nor detached.

The two main issues are:
-Notification when the quicker result has arrived.
-Terminating (and cleanup up) of the slower thread.

  • 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-12T17:03:48+00:00Added an answer on June 12, 2026 at 5:03 pm

    NoSenseEtAl provided a link to:

    http://fpcomplete.com/functional-patterns-in-c/

    There Bartosz provides example code of some very generic composable async API’s. I shamelessly stripped it down to only what I needed (and could understand). Just the “Async Or-Combinator”:

    #include <functional>
    #include <iostream>
    #include <string>
    
    #include <memory>
    #include <algorithm>
    #include <ctype.h>
    
    #include <thread>
    #include <mutex>
    #include <chrono>
    
    #include <random>
    
    using namespace std;
    
    //--------
    // Helpers
    //--------
    
    void tick(int n)
    {
        for(int i = 0; i < n; ++i)
        {
            cout << i << endl;
            this_thread::sleep_for(chrono::seconds(1));
        }
    }
    
    //-------
    // Async
    //-------
    
    template<class A>
    struct Async {
        virtual ~Async() {}
        virtual void andThen(function<void(A)>) = 0;
    };
    
    //-------
    // Monoid
    //-------
    
    template<class A>
    struct Mplus : Async<A>
    {
        Mplus(unique_ptr<Async<A>> asnc1, unique_ptr<Async<A>> asnc2) 
        : _asnc1(move(asnc1)), _asnc2(move(asnc2)), _done(false)
        {}
        ~Mplus() {}
        void andThen(function<void(A)> k)
        {
            _asnc1->andThen([this, k](A a)
            {
                lock_guard<mutex> l(_mtx);
                if (!_done)
                {
                    _done = true;
                    k(a);
                }
            });
            _asnc2->andThen([this, k](A a)
            {
                lock_guard<mutex> l(_mtx);
                if (!_done)
                {
                    _done = true;
                    k(a);
                }
            });
        }
        unique_ptr<Async<A>> _asnc1;
        unique_ptr<Async<A>> _asnc2;
        bool _done;
        mutex _mtx;
    };
    
    template<class A>
    unique_ptr<Async<A>> mplus(unique_ptr<Async<A>> asnc1, unique_ptr<Async<A>> asnc2)
    {
        return unique_ptr<Async<A>>(new Mplus<A>(move(asnc1), move(asnc2)));
    }
    
    //----------------
    // Fake async APIs
    //----------------
    
    void getStringAsync(string s, function<void(string)> handler)
    {
        thread th([s, handler]()
        {
            cout << "Started async\n";
    
            size_t sleep = rand () % 10;
            this_thread::sleep_for(chrono::seconds(sleep));
    
            handler("Done async: " + s);
        });
        th.detach();
    }
    
    struct AsyncString : Async<string>
    {
        AsyncString(string s) : _s(s) {}
        void andThen(function<void(string)> k)
        {
            getStringAsync(_s, k);
        }
        string _s;
    };
    
    unique_ptr<Async<string>> asyncString(string s)
    {
        return unique_ptr<Async<string>>(new AsyncString(s));
    }
    
    void testOr()
    {
        // Test or combinator / mplus
        auto or = mplus<string>(asyncString(" Result One "), asyncString(" Result Two "));
        or->andThen([](string s)
        {
            cout << "Or returned : " << s << endl;
        });
    
        tick(10);
    }
    
    void main()
    {
        srand ( time(NULL) );
    
        testOr();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would one go about implementing the same type of marker grouping as found
How would one go about proving to management that a batch reformat of all
How would one go about writing a query that selects items 2000-2010 out of
How would one go about implementing the indexing operator on a class in C#
How would one go about implementing a who's online feature using PHP? Of course,
How would one go about either using the 3D components of WPF or using
How would one go about defining something like this in an xsd? <start> <request
How would one go about specifying an offset for auto-naming the individual output files?
How exactly would one go about getting an OpenGL app to run fullscreen straight
Conceptually speaking how would one go about writing an achievement system for a website

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.