From d37f85f728554f7d8910ad00f9906b312e9433b5 Mon Sep 17 00:00:00 2001 From: marcelb Date: Tue, 10 Jun 2025 11:32:21 +0200 Subject: [PATCH] Limit Nonblocking loop await --- lib/asynco.hpp | 22 +++++++++++++++ test/main.cpp | 76 +++++++++++++++++++++++++------------------------- 2 files changed, 60 insertions(+), 38 deletions(-) diff --git a/lib/asynco.hpp b/lib/asynco.hpp index dbe0de1..3fac408 100644 --- a/lib/asynco.hpp +++ b/lib/asynco.hpp @@ -68,6 +68,28 @@ T await_(future&& r) { return move(r).get(); } +/** + * Block until the asynchronous call completes +*/ +template +T await_(future& r, uint32_t time_us = 10) { + while (r.wait_for(std::chrono::microseconds(time_us)) != std::future_status::ready) { + _asynco_engine.io_context.poll_one(); + } + return r.get(); +} + +/** + * Block until the asynchronous call completes +*/ +template +T await_(future&& r, uint32_t time_us = 10) { + while (r.wait_for(std::chrono::microseconds(time_us)) != std::future_status::ready) { + _asynco_engine.io_context.poll_one(); + } + return move(r).get(); +} + /** * Run the function asynchronously an block until completes */ diff --git a/test/main.cpp b/test/main.cpp index ecc5fa5..0088a6e 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -17,22 +17,22 @@ using namespace marcelb::asynco; using namespace std; using namespace this_thread; -asyncable c2 (int a) { - co_return a*2; -} +// asyncable c2 (int a) { +// co_return a*2; +// } -asyncable sleep_co (int a) { - sleep(a); - cout << "Gotov" << endl; - co_return; -} +// asyncable sleep_co (int a) { +// sleep(a); +// cout << "Gotov" << endl; +// co_return; +// } -asyncable c () { - cout << "Ispisi" << endl; - co_await c2(0); - co_return; -} +// asyncable c () { +// cout << "Ispisi" << endl; +// co_await c2(0); +// co_return; +// } @@ -524,35 +524,35 @@ int main () { // co_return; // }()); - async_ ([]() -> asyncable { - cout << "1" << endl; - co_await sleep_co(1); - co_return; - }()); + // async_ ([]() -> asyncable { + // cout << "1" << endl; + // co_await sleep_co(1); + // co_return; + // }()); - async_ ([]() -> asyncable { - cout << "2" << endl; - co_await sleep_co(1); - co_return; - }()); + // async_ ([]() -> asyncable { + // cout << "2" << endl; + // co_await sleep_co(1); + // co_return; + // }()); - async_ ([]() -> asyncable { - cout << "3" << endl; - co_await sleep_co(1); - co_return; - }()); + // async_ ([]() -> asyncable { + // cout << "3" << endl; + // co_await sleep_co(1); + // co_return; + // }()); - async_ ([]() -> asyncable { - cout << "4" << endl; - co_await sleep_co(1); - co_return; - }()); + // async_ ([]() -> asyncable { + // cout << "4" << endl; + // co_await sleep_co(1); + // co_return; + // }()); - async_ ([]() -> asyncable { - cout << "5" << endl; - co_await sleep_co(1); - co_return; - }()); + // async_ ([]() -> asyncable { + // cout << "5" << endl; + // co_await sleep_co(1); + // co_return; + // }()); // await_ ([]() {