mercredi 4 décembre 2019

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?

Aucun commentaire:

Enregistrer un commentaire