From 8d9a56dc8e184804a6732856736010dcc076d149 Mon Sep 17 00:00:00 2001 From: marcelb Date: Wed, 11 Jun 2025 19:32:40 +0200 Subject: [PATCH] Test, debug and fix coroutine --- README.md | 8 ++++---- lib/asynco_default.hpp | 4 ---- src/asynco.cpp | 1 - test/main_coroutine.cpp | 13 +++++++------ test/main_coroutine_default.cpp | 11 ++++++----- test/main_timers_default.cpp | 4 ++-- 6 files changed, 19 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 115bbef..06e6b29 100644 --- a/README.md +++ b/README.md @@ -311,11 +311,11 @@ mt.tick("string", string("Hello world")); ## Coroutine -If `define.hpp` is included, you can initialize coroutines using `asyncable`; if not, just use `boost::asio::awaitable`. +If `define.hpp` is included, you can initialize coroutines with `boost::asio::awaitable`. ```c++ -asyncable c2(int a) { +awaitable c2(int a) { co_return a * 2; } @@ -330,7 +330,7 @@ Or using a lambda expression: ```c++ -async_([]() -> asyncable { +async_([]() -> awaitable { std::cout << "Hello" << std::endl; co_await c2(4); co_return; @@ -354,7 +354,7 @@ If you need the result immediately, you can use a shorter notation auto a = await_ ( c2(3)); cout << a << endl; -await_ ([]() -> asyncable { +await_ ([]() -> awaitable { cout << "Hello" << endl; co_return; }()); diff --git a/lib/asynco_default.hpp b/lib/asynco_default.hpp index 7577abd..246fb94 100644 --- a/lib/asynco_default.hpp +++ b/lib/asynco_default.hpp @@ -97,10 +97,6 @@ Trigger trigger() { #define async_ marcelb::asynco::async_ #define await_ marcelb::asynco::await_ -#if __cplusplus >= 202002L -#define asyncable boost::asio::awaitable -#endif - Asynco& asynco_default_runtime(); void asynco_default_run(); diff --git a/src/asynco.cpp b/src/asynco.cpp index 7a14554..d2cfaaa 100644 --- a/src/asynco.cpp +++ b/src/asynco.cpp @@ -18,7 +18,6 @@ void Asynco::run(uint8_t threads) { void Asynco::run_on_this() { if (!_work) { - cout << "POKRENE SE KREIRANJE WORK PTR"; _work = make_unique(io_ctx); } io_ctx.run(); diff --git a/test/main_coroutine.cpp b/test/main_coroutine.cpp index 2c2ecc4..81358b7 100644 --- a/test/main_coroutine.cpp +++ b/test/main_coroutine.cpp @@ -4,18 +4,19 @@ using namespace marcelb::asynco; #include using namespace std; +awaitable c2(int a) { + co_return a * 2; +} + + int main() { Asynco asynco; // or global asynco.run(2); - asyncable c2(int a) { - co_return a * 2; - } - asynco.async(c2(4)); - asynco.async([]() -> asyncable { + asynco.async([]() -> awaitable { std::cout << "Hello" << std::endl; co_await c2(4); co_return; @@ -31,7 +32,7 @@ int main() { auto a = asynco.await( c2(3)); cout << a << endl; - asynco.await([]() -> asyncable { + asynco.await([]() -> awaitable { cout << "Hello" << endl; co_return; }()); diff --git a/test/main_coroutine_default.cpp b/test/main_coroutine_default.cpp index e7315ae..f881cda 100644 --- a/test/main_coroutine_default.cpp +++ b/test/main_coroutine_default.cpp @@ -4,16 +4,17 @@ using namespace marcelb::asynco; #include using namespace std; +awaitable c2(int a) { + co_return a * 2; +} + int main() { asynco_default_run(); - asyncable c2(int a) { - co_return a * 2; - } async_(c2(4)); - async_([]() -> asyncable { + async_([]() -> awaitable { std::cout << "Hello" << std::endl; co_await c2(4); co_return; @@ -29,7 +30,7 @@ int main() { auto a = await_ ( c2(3)); cout << a << endl; - await_ ([]() -> asyncable { + await_ ([]() -> awaitable { cout << "Hello" << endl; co_return; }()); diff --git a/test/main_timers_default.cpp b/test/main_timers_default.cpp index d6a3f82..adb11bf 100644 --- a/test/main_timers_default.cpp +++ b/test/main_timers_default.cpp @@ -13,7 +13,7 @@ int main() { }, 1000); // stop periodic - inter1.stop(); + // inter1.stop(); // how many times it has expired int ti = inter1.ticks(); @@ -27,7 +27,7 @@ int main() { }, 10000); // stop delayed - time1.stop(); + // time1.stop(); // is it expired int tt = time1.expired();