shunyo
shunyo

Reputation: 1307

Using std::async for function call from thread?

I am running two parallel threads. One of the threads need to have an asynchronous function call upon the fulfillment of a conditional statement. I have found out that std::async performs asynchronous function call using the launch policies, but I have a few questions regarding them.

  1. Is there a policy to make it wait for a conditional statement to happen? According to what I have understood from this post, there are a variety of wait_for and wait_until functions, but I have found them to take in a time function, can these be suitably modified?

  2. Will there be automatic destructor call at the end of the async function?

  3. Will the function call affect the parent thread's functioning in any manner?

Upvotes: 1

Views: 356

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490038

When you call std::async, you pass it the address of a function to call (along with any parameters you want to pass to that function).

It then creates a thread to execute that function asynchronously. It returns a future, which the parent thread can use to get the result from the child. Typical usage is something like this:

#include <string>
#include <future>
#include <iostream>
#include <chrono>

std::chrono::seconds sec(1);

int process() {
    std::cerr << "Doing something slow\n";
    std::this_thread::sleep_for(sec);
    std::cerr << "done\n";
    return 1;
}

int main(int argc, char **argv) {
    if (argc > 1) {
        auto func = std::async(process);
        std::cerr << "doing something else that takes a while\n";
        std::this_thread::sleep_for(sec);
        func.get();
    }
}

Note that we only have to use .get on the returned future to synchronize the threads. The sleep_for is just to simulate each thread doing something that takes at least a little while--if they finished too quickly, they wouldn't get a chance to really execute in parallel, since the first to run could finish and exit before the second got a chance to start running at all.

If you want to create explicit threads (i.e., create instances of std::thread), that's when you end up using std::wait_for and such (or can end up using them, anyway). With futures (i.e., what you create with std::async) you just use .get to wait for the thread to finish and retrieve whatever the thread function returned.

Upvotes: 2

Related Questions