Timed await

multiple_init_problem
mbandic 1 month ago
parent 9e773f55c9
commit 3fc313a9b5
  1. 4
      .vscode/settings.json
  2. 22
      lib/asynco.hpp

@ -69,6 +69,8 @@
"cinttypes": "cpp", "cinttypes": "cpp",
"typeindex": "cpp", "typeindex": "cpp",
"typeinfo": "cpp", "typeinfo": "cpp",
"variant": "cpp" "variant": "cpp",
"coroutine": "cpp",
"source_location": "cpp"
} }
} }

@ -35,6 +35,28 @@ T await_(future<T>&& r) {
return move(r).get(); return move(r).get();
} }
/**
* Block until the asynchronous call completes or time expired
*/
template<typename T>
T await_(future<T>& r, uint64_t time) {
if (r.wait_for(chrono::milliseconds(time)) == std::future_status::timeout) {
throw runtime_error("Asynchronous execution timed out");
}
return r.get();
}
/**
* Block until the asynchronous call completes or time expired
*/
template<typename T>
T await_(future<T>&& r, uint64_t time) {
if (r.wait_for(chrono::milliseconds(time)) == std::future_status::timeout) {
throw runtime_error("Asynchronous execution timed out");
}
return move(r).get();
}
} }
} }

Loading…
Cancel
Save