访问已删除的对象不会导致崩溃

时间:2018-05-20 20:09:37

标签: c++ multithreading asynchronous vector

请考虑以下代码:

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");
}

当我运行程序时,它打印end of f然后等待2秒然后打印0 1 2 3 4 5 6 7 8 9。 我的问题是 - 如果我将V作为std::ref的引用传递,并且在f()的范围之后将其删除。那么在f()之后删除V时程序如何推送和访问V?

0 个答案:

没有答案
相关问题