c++ - Is it safe to return from function before all std::futures are finished? -
i have code that:
int function() { std::vector<std::future<int>> futures; (const auto& elem : elements) { futures.push_back(std::async(&myclass::foo, myclass, elem); } (auto& f : futures) { const int x = f.get(); if (x != 0) return x; } }
can return function when there unfinished async calls? i'm interested in 1 non 0 value. should wait until async calls finished? code safe?
the destructor of std::future
(when initialized call std::async
) blocks until async task complete. (see here)
so function return
statement not complete until of tasks have finished.
Comments
Post a Comment