多次使用std::future.get()会发生崩溃, 其为一次性事件,只能使用一次。
int main(void)
{
std::promise<int> pr;
auto fu = pr.get_future();
std::thread t([&pr]() {
int i = 0;
while (true)
{
this_thread::sleep_for(chrono::milliseconds(100));
pr.set_value(i++);
}
});
while (true)
{
this_thread::sleep_for(chrono::milliseconds(100));
cout << fu.get() << endl;
}
return 0;
}