From 3fc313a9b578cf20363266f912f76411f2be6605 Mon Sep 17 00:00:00 2001 From: mbandic Date: Thu, 17 Oct 2024 13:07:08 +0000 Subject: [PATCH] Timed await --- .vscode/settings.json | 4 +++- lib/asynco.hpp | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index aaed338..8372c4a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -69,6 +69,8 @@ "cinttypes": "cpp", "typeindex": "cpp", "typeinfo": "cpp", - "variant": "cpp" + "variant": "cpp", + "coroutine": "cpp", + "source_location": "cpp" } } \ No newline at end of file diff --git a/lib/asynco.hpp b/lib/asynco.hpp index d57ec0f..f06a492 100644 --- a/lib/asynco.hpp +++ b/lib/asynco.hpp @@ -35,6 +35,28 @@ T await_(future&& r) { return move(r).get(); } +/** + * Block until the asynchronous call completes or time expired +*/ +template +T await_(future& 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 +T await_(future&& 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(); +} + } }