Limit Nonblocking loop await

This commit is contained in:
marcelb 2025-06-10 11:32:21 +02:00
parent 5608eab8eb
commit d37f85f728
2 changed files with 60 additions and 38 deletions

View File

@ -68,6 +68,28 @@ T await_(future<T>&& r) {
return move(r).get();
}
/**
* Block until the asynchronous call completes
*/
template<typename T>
T await_(future<T>& 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<typename T>
T await_(future<T>&& 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
*/

View File

@ -17,22 +17,22 @@ using namespace marcelb::asynco;
using namespace std;
using namespace this_thread;
asyncable<int> c2 (int a) {
co_return a*2;
}
// asyncable<int> c2 (int a) {
// co_return a*2;
// }
asyncable<void> sleep_co (int a) {
sleep(a);
cout << "Gotov" << endl;
co_return;
}
// asyncable<void> sleep_co (int a) {
// sleep(a);
// cout << "Gotov" << endl;
// co_return;
// }
asyncable<void> c () {
cout << "Ispisi" << endl;
co_await c2(0);
co_return;
}
// asyncable<void> c () {
// cout << "Ispisi" << endl;
// co_await c2(0);
// co_return;
// }
@ -524,35 +524,35 @@ int main () {
// co_return;
// }());
async_ ([]() -> asyncable<void> {
cout << "1" << endl;
co_await sleep_co(1);
co_return;
}());
// async_ ([]() -> asyncable<void> {
// cout << "1" << endl;
// co_await sleep_co(1);
// co_return;
// }());
async_ ([]() -> asyncable<void> {
cout << "2" << endl;
co_await sleep_co(1);
co_return;
}());
// async_ ([]() -> asyncable<void> {
// cout << "2" << endl;
// co_await sleep_co(1);
// co_return;
// }());
async_ ([]() -> asyncable<void> {
cout << "3" << endl;
co_await sleep_co(1);
co_return;
}());
// async_ ([]() -> asyncable<void> {
// cout << "3" << endl;
// co_await sleep_co(1);
// co_return;
// }());
async_ ([]() -> asyncable<void> {
cout << "4" << endl;
co_await sleep_co(1);
co_return;
}());
// async_ ([]() -> asyncable<void> {
// cout << "4" << endl;
// co_await sleep_co(1);
// co_return;
// }());
async_ ([]() -> asyncable<void> {
cout << "5" << endl;
co_await sleep_co(1);
co_return;
}());
// async_ ([]() -> asyncable<void> {
// cout << "5" << endl;
// co_await sleep_co(1);
// co_return;
// }());
// await_ ([]() {