Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can std::async int a function quit out before task is finished?

my code is below:

void f1() {
    for (int i = 0; i < 1000; ++i) {
        std::cout << "f1: " << i << std::endl;
    }
}

void f2() {
    for (int i = 0; i < 10; ++i) {
        std::cout << "f2: " << i << std::endl;
    }
}
auto fz = []() {
        auto l_future = std::async(std::launch::async, f1);
        auto r_future = std::async(std::launch::async, f2);
        while (!is_ready(r_future)) {}
        std::cout << "right done" << std::endl;
    };

fz();
std::cout << "one of task done" << std::endl;

the result is that "right done" is printed but fz() is not finished."one of task done" is printed until the f1 is finished. Now i want to print "one of task done" before f1 is finished.How can i do?

like image 536
haskeller Avatar asked Dec 04 '19 08:12

haskeller


People also ask

How does STD async work?

std::async() does following things, It automatically creates a thread (Or picks from internal thread pool) and a promise object for us. Then passes the std::promise object to thread function and returns the associated std::future object.

How does C++ async work?

As the name indicates, C++ async is a function template fn, which takes functions or function objects as arguments (basically called callbacks) and runs them asynchronously. It returns the std:: the future object which is used to keep the result of the above function. The result is stored in the shared state.

How do you write async function in C++?

Just do: // This function is called asynchronously std::future<int> EventualValue = std::async(std::launch::async, MyFunc, x, y);


Video Answer


2 Answers

From this std::async reference:

If the std::future obtained from std::async is not moved from or bound to a reference, the destructor of the std::future will block at the end of the full expression until the asynchronous operation completes

When the lambda ends and the two std::future objects are destructed, the destruction of l_future will block until the function f1 returns.

like image 131
Some programmer dude Avatar answered Oct 11 '22 14:10

Some programmer dude


Your code hangs in [1] line

auto fz = []() {
        auto l_future = std::async(std::launch::async, f1);
        auto r_future = std::async(std::launch::async, f2);
        while (!is_ready(r_future)) {}
        std::cout << "right done" << std::endl;
        // [1]
    };

because dtor of future waits for a result.

You can return future from your lambda, and after cout call get to wait for a result i.e. until f1 task is completion:

auto fz = []() {
        auto l_future = std::async(std::launch::async, f1);
        auto r_future = std::async(std::launch::async, f2);
        while (!is_ready(r_future)) {}
        std::cout << "right done" << std::endl;
        return l_future; // move future outside lambda
    };

auto fut = fz();
std::cout << "one of task done" << std::endl;
fut.get();
like image 42
rafix07 Avatar answered Oct 11 '22 12:10

rafix07