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

  • Home
  • SEARCH
  • 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 7068285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:18:19+00:00 2026-05-28T05:18:19+00:00

Without using boost::thread and boost::bind directly, is there a way to implement the equivalent

  • 0

Without using boost::thread and boost::bind directly, is there a way to implement the equivalent of the following code?

std::string func()
{
    std::string str("Hello from async task!");
    return str;
}

int main()
{
    auto ftr = std::async(&func);
    std::cout << "Hello from main!";
    std::string str = ftr.get();
    std::cout << str << std::endl;      
    return 0;
}

Specifically, this part: auto ftr = std::async(&func); ?

  • 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-28T05:18:20+00:00Added an answer on May 28, 2026 at 5:18 am

    Certainly. Simply make async<T>(std::function<T()>) return a future which invokes func() the moment it’s first waited-for. You won’t get any asynchronicity, but the API doesn’t actually guarantee that the function will run asynchronously, so that’s not a problem.

    If you have access to an OS-specific threading library, you could of course use that as well.

    Note, however, that storing exceptions cannot be implemented portably; it requires additional support from the C++ implementation, unless you can restrict the exceptions supported to ones with a polymorphic clone function. See this question for more information.

    The final implementation might look a bit like this (untested):

    // NOTE - we assume a SINGLE THREADED environment
    
    template<typename T>
    class myfuture_detail {
        mutable boost::variant<T, boost::function<T()> > val;
    
    public:
        myfuture_detail(const boost::function<T()> &f)
            : val(f) { }
    
        const T &get() const {
            if (T *t = boost::get<T>(&val)) {
                return *t;
            } else {
                boost::function<T()> f = *boost::get<boost::function<T> >(&val);
                val = f();
    
                T *t = boost::get<T>(&val);
                assert(t);
    
                return *t;
            }
        }
    };
    
    template<typename T>
    class myfuture {
        boost::shared_ptr<myfuture_detail<T> > ptr;
    
    public:
        myfuture(const boost::function<T()> &f)
            : ptr(boost::make_shared<myfuture_detail<T> >(f))
        {}
    
        myfuture() { }
    
        const T &get() const {
            return ptr->get();
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way without using extra space to find LCA of nary tree.
Is there any way to use the += operator with a vector without using
Using Boost.Python, is there a way to call a Python function that's been passed
Is there a similar way to write this regex without using possessive quantifiers (ie
Without using jQuery, what is the best way to limit text entry of a
I wrote code first without using functions to prototype, and of course, it worked
I wrote a small tcp client using boost::asio, providing the following function: typedef boost::function<void(const
I'm using boost (signals + bind) and c++ for passing function reference. Here is
I'm writing some code with boost::asio , using asynchronous TCP connections. I've to admit
I want to do that without using boost and strange things, I want to

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.