Consider the following code:
void g(std::vector<int>& V)
{
std::this_thread::sleep_for(std::chrono::seconds(2));
for (int i = 0; i < 100; ++i) { V.push_back(i); }
for (int i = 0; i < 100; ++i) { std::cout<<V[i]<<" "; }
}
void f()
{
std::vector<int> V;
auto fut = std::async(std::launch::async, g, std::ref(V));
std::cout << "end of f" << std::endl;
}
int main() {
f();
system("pause");
}
When I run the program, it prints end of f
then waits 2 second then prints 0 1 2 3 4 5 6 7 8 9
.
My question is - if I pass V as a reference with std::ref
and after the scope of f()
it should be deleted. So how the program pushes and access to V when V is deleted after f()?