Imagine the following scenario:
想象一下以下场景:
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
void DoSomething(int* i)
{
std::cout << *i << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
*i = 2;
std::cout << *i << std::endl;
}
int main()
{
std::vector<int> v = {0, 0, 0};
v[0] = 1;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::thread t(&DoSomething, &v[0]);
t.join();
std::cout << v[0] << std::endl;
}
#