From 4283f328263b1525b671374598b5833584622177 Mon Sep 17 00:00:00 2001 From: marcelb Date: Mon, 29 Sep 2025 13:08:40 +0200 Subject: [PATCH] Work on loop like in Rust.. ok in default --- CMakeLists.txt | 1 + lib/asynco.hpp | 8 +++ lib/asynco_default.hpp | 6 ++ src/asynco.cpp | 8 +++ src/asynco_default.cpp | 8 +++ test/CMakeLists.txt | 8 ++- test/main_async_default.cpp | 11 ++- test/main_infinit_loop.cpp | 111 +++++++++++++++++++++++++++++ test/main_infinit_loop_default.cpp | 111 +++++++++++++++++++++++++++++ 9 files changed, 270 insertions(+), 2 deletions(-) create mode 100644 test/main_infinit_loop.cpp create mode 100644 test/main_infinit_loop_default.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index db73881..e4cc5ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ add_subdirectory(test) add_compile_options(-w) +# add_definitions(-DASYNCO_THREADS_POOL_SIZE=20) # Instaliraj biblioteku diff --git a/lib/asynco.hpp b/lib/asynco.hpp index f70c97e..3db9e7b 100644 --- a/lib/asynco.hpp +++ b/lib/asynco.hpp @@ -18,6 +18,8 @@ using namespace std; #endif using namespace boost::asio; +#define loop while(true) + #include "timers.hpp" #include "trigger.hpp" @@ -170,6 +172,12 @@ public: Timer periodic(function callback, uint64_t time); + /** + * Nonblock time sleep function + */ + + void sleep(int _time); + /** * Initialize trigger (typed event) */ diff --git a/lib/asynco_default.hpp b/lib/asynco_default.hpp index 441259e..408807c 100644 --- a/lib/asynco_default.hpp +++ b/lib/asynco_default.hpp @@ -97,6 +97,12 @@ Timer delayed(function callback, uint64_t time); Timer periodic(function callback, uint64_t time); +/** + * Nonblock time sleep function + */ + +void sleep(int _time); + /** * Initialize trigger (typed event) */ diff --git a/src/asynco.cpp b/src/asynco.cpp index d2cfaaa..61ea83b 100644 --- a/src/asynco.cpp +++ b/src/asynco.cpp @@ -37,5 +37,13 @@ Timer Asynco::periodic(function callback, uint64_t time) { return Timer(io_ctx, callback, time, TimerType::Periodic); } +void Asynco::sleep(int _time) { + promise _promise; + Timer t = delayed( [&]() { + _promise.set_value(); + }, _time); + + return await(_promise.get_future()); +} }; diff --git a/src/asynco_default.cpp b/src/asynco_default.cpp index 3f06e74..a34e0ff 100644 --- a/src/asynco_default.cpp +++ b/src/asynco_default.cpp @@ -13,12 +13,20 @@ Timer periodic(function callback, uint64_t time) { return Timer(Asynco_Default_Runtime.io_ctx, callback, time, TimerType::Periodic); } +void sleep(int _time) { + return Asynco_Default_Runtime.sleep(_time); +} + Asynco& asynco_default_runtime() { return Asynco_Default_Runtime; } void asynco_default_run() { +#ifdef ASYNCO_THREADS_POOL_SIZE + Asynco_Default_Runtime.run(ASYNCO_THREADS_POOL_SIZE); +#else Asynco_Default_Runtime.run(); +#endif } void asynco_default_run_on_this() { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 729fb9a..8617b21 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,4 +26,10 @@ add_executable(asynco_coroutine_default main_coroutine_default.cpp) target_link_libraries(asynco_coroutine_default asynco Boost::system) add_executable(asynco_coroutine main_coroutine.cpp) -target_link_libraries(asynco_coroutine asynco Boost::system) \ No newline at end of file +target_link_libraries(asynco_coroutine asynco Boost::system) + +add_executable(asynco_infinit_loop_default main_infinit_loop_default.cpp) +target_link_libraries(asynco_infinit_loop_default asynco Boost::system) + +add_executable(asynco_infinit_loop main_infinit_loop.cpp) +target_link_libraries(asynco_infinit_loop asynco Boost::system) \ No newline at end of file diff --git a/test/main_async_default.cpp b/test/main_async_default.cpp index 569996f..a613921 100644 --- a/test/main_async_default.cpp +++ b/test/main_async_default.cpp @@ -11,8 +11,15 @@ void notLambdaFunction() { class clm { public: + int i = 7; void classMethode() { - cout << "Call class method" << endl; + cout << "Call class method" << i << endl; + } + + future asyncMethode() { + return async_([&]() { + cout << "Async class method" << i << endl; + }); } }; @@ -53,6 +60,8 @@ int main() { classes.classMethode(); }); + await_(classes.asyncMethode()); + //------------------AWAIT---------------------- auto a = async_ ( []() { diff --git a/test/main_infinit_loop.cpp b/test/main_infinit_loop.cpp new file mode 100644 index 0000000..639d637 --- /dev/null +++ b/test/main_infinit_loop.cpp @@ -0,0 +1,111 @@ +#include "../lib/asynco.hpp" +using namespace marcelb::asynco; + +#include +using namespace std; + + +int main() { + Asynco asynco; + asynco.run(4); + + asynco.async ([&](){ + loop { + cout << "Loop 1" << endl; + asynco.sleep(1000); + } + }); + + + asynco.async ([&](){ + loop { + cout << "Loop 2" << endl; + asynco.sleep(2000); + } + }); + + + asynco.async ([&](){ + loop { + cout << "Loop 25" << endl; + asynco.sleep(2500); + } + }); + + + asynco.async ([&](){ + loop { + cout << "Loop 3" << endl; + asynco.sleep(3000); + } + }); + + + asynco.async ([&](){ + loop { + cout << "Loop 35" << endl; + asynco.sleep(3500); + } + }); + + + asynco.async ([&](){ + loop { + cout << "Loop 4" << endl; + asynco.sleep(4000); + } + }); + + + asynco.async ([&](){ + loop { + cout << "Loop 45" << endl; + asynco.sleep(4500); + } + }); + + + asynco.async ([&](){ + loop { + cout << "Loop 5" << endl; + asynco.sleep(5000); + } + }); + + + asynco.async ([&](){ + loop { + cout << "Loop 55" << endl; + asynco.sleep(5500); + } + }); + + asynco.async ([&](){ + loop { + cout << "Loop 6" << endl; + asynco.sleep(6000); + } + }); + + asynco.async ([&](){ + loop { + cout << "Loop 65" << endl; + asynco.sleep(6500); + } + }); + + asynco.async ([&](){ + loop { + cout << "Loop 7" << endl; + asynco.sleep(7000); + } + }); + + loop { // blokira trenutnu + cout << "Loop 15" << endl; + asynco.sleep(1500); + } + + asynco.join(); + return 0; +} \ No newline at end of file diff --git a/test/main_infinit_loop_default.cpp b/test/main_infinit_loop_default.cpp new file mode 100644 index 0000000..18d2a77 --- /dev/null +++ b/test/main_infinit_loop_default.cpp @@ -0,0 +1,111 @@ +#include "../lib/asynco_default.hpp" +using namespace marcelb::asynco; + +#include +using namespace std; + + +int main() { + asynco_default_run(); + + + async_ ([](){ + loop { + cout << "Loop 1" << endl; + sleep(1000); + } + }); + + + async_ ([](){ + loop { + cout << "Loop 2" << endl; + sleep(2000); + } + }); + + + async_ ([](){ + loop { + cout << "Loop 25" << endl; + sleep(2500); + } + }); + + + async_ ([](){ + loop { + cout << "Loop 3" << endl; + sleep(3000); + } + }); + + + async_ ([](){ + loop { + cout << "Loop 35" << endl; + sleep(3500); + } + }); + + + async_ ([](){ + loop { + cout << "Loop 4" << endl; + sleep(4000); + } + }); + + + async_ ([](){ + loop { + cout << "Loop 45" << endl; + sleep(4500); + } + }); + + + async_ ([](){ + loop { + cout << "Loop 5" << endl; + sleep(5000); + } + }); + + + async_ ([](){ + loop { + cout << "Loop 55" << endl; + sleep(5500); + } + }); + + async_ ([](){ + loop { + cout << "Loop 6" << endl; + sleep(6000); + } + }); + + async_ ([](){ + loop { + cout << "Loop 65" << endl; + sleep(6500); + } + }); + + async_ ([](){ + loop { + cout << "Loop 7" << endl; + sleep(7000); + } + }); + + loop { // blokira trenutnu + cout << "Loop 15" << endl; + sleep(1500); + } + + asynco_default_join(); + return 0; +} \ No newline at end of file