diff --git a/lib/asynco.hpp b/lib/asynco.hpp index 3db9e7b..4dff705 100644 --- a/lib/asynco.hpp +++ b/lib/asynco.hpp @@ -26,6 +26,12 @@ using namespace boost::asio; namespace marcelb { namespace asynco { + +struct SleepHandle { + std::future future; + std::shared_ptr timer; +}; + /** * Asynco runtime * Used for all asynchronous capabilities of this wrapper @@ -177,6 +183,9 @@ public: */ void sleep(int _time); + + SleepHandle sleep2(int _time); + /** * Initialize trigger (typed event) diff --git a/lib/asynco_default.hpp b/lib/asynco_default.hpp index 408807c..f5fbdae 100644 --- a/lib/asynco_default.hpp +++ b/lib/asynco_default.hpp @@ -102,6 +102,7 @@ Timer periodic(function callback, uint64_t time); */ void sleep(int _time); +SleepHandle sleep2(int _time); /** * Initialize trigger (typed event) diff --git a/src/asynco.cpp b/src/asynco.cpp index 61ea83b..30a9fe3 100644 --- a/src/asynco.cpp +++ b/src/asynco.cpp @@ -46,4 +46,14 @@ void Asynco::sleep(int _time) { return await(_promise.get_future()); } +SleepHandle Asynco::sleep2(int _time) { + auto _promise = std::make_shared>(); + + auto _timer = std::make_shared(io_ctx, [_promise]() { + _promise->set_value(); + }, _time, TimerType::Delayed); + + return { _promise->get_future(), _timer }; +} + }; diff --git a/src/asynco_default.cpp b/src/asynco_default.cpp index a34e0ff..bf3b3ce 100644 --- a/src/asynco_default.cpp +++ b/src/asynco_default.cpp @@ -17,6 +17,11 @@ void sleep(int _time) { return Asynco_Default_Runtime.sleep(_time); } +SleepHandle sleep2(int _time) { + return Asynco_Default_Runtime.sleep2(_time); +} + + Asynco& asynco_default_runtime() { return Asynco_Default_Runtime; } diff --git a/test/main_infinit_loop.cpp b/test/main_infinit_loop.cpp index 639d637..7391f19 100644 --- a/test/main_infinit_loop.cpp +++ b/test/main_infinit_loop.cpp @@ -9,102 +9,102 @@ int main() { Asynco asynco; asynco.run(4); - asynco.async ([&](){ - loop { - cout << "Loop 1" << endl; - asynco.sleep(1000); - } - }); + // 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 2" << endl; + // asynco.sleep(2000); + // } + // }); - asynco.async ([&](){ - loop { - cout << "Loop 25" << endl; - asynco.sleep(2500); - } - }); + // 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 3" << endl; + // asynco.sleep(3000); + // } + // }); - asynco.async ([&](){ - loop { - cout << "Loop 35" << endl; - asynco.sleep(3500); - } - }); + // 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 4" << endl; + // asynco.sleep(4000); + // } + // }); - asynco.async ([&](){ - loop { - cout << "Loop 45" << endl; - asynco.sleep(4500); - } - }); + // 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 5" << endl; + // asynco.sleep(5000); + // } + // }); - asynco.async ([&](){ - loop { - cout << "Loop 55" << endl; - asynco.sleep(5500); - } - }); + // 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 6" << endl; + // asynco.sleep(6000); + // } + // }); - asynco.async ([&](){ - loop { - cout << "Loop 65" << endl; - asynco.sleep(6500); - } - }); + // asynco.async ([&](){ + // loop { + // cout << "Loop 65" << endl; + // asynco.sleep(6500); + // } + // }); - asynco.async ([&](){ - loop { - cout << "Loop 7" << endl; - asynco.sleep(7000); - } - }); + // asynco.async ([&](){ + // loop { + // cout << "Loop 7" << endl; + // asynco.sleep(7000); + // } + // }); - loop { // blokira trenutnu - cout << "Loop 15" << endl; - asynco.sleep(1500); - } + // loop { // blokira trenutnu + // cout << "Loop 15" << endl; + // asynco.sleep(1500); + // } asynco.join(); return 0; diff --git a/test/main_infinit_loop_default.cpp b/test/main_infinit_loop_default.cpp index 18d2a77..ac2b83d 100644 --- a/test/main_infinit_loop_default.cpp +++ b/test/main_infinit_loop_default.cpp @@ -12,7 +12,7 @@ int main() { async_ ([](){ loop { cout << "Loop 1" << endl; - sleep(1000); + await_(sleep2(1000).future); } }); @@ -20,7 +20,7 @@ int main() { async_ ([](){ loop { cout << "Loop 2" << endl; - sleep(2000); + await_(sleep2(2000).future); } }); @@ -28,7 +28,7 @@ int main() { async_ ([](){ loop { cout << "Loop 25" << endl; - sleep(2500); + await_(sleep2(2500).future); } }); @@ -36,7 +36,7 @@ int main() { async_ ([](){ loop { cout << "Loop 3" << endl; - sleep(3000); + await_(sleep2(3000).future); } }); @@ -44,7 +44,7 @@ int main() { async_ ([](){ loop { cout << "Loop 35" << endl; - sleep(3500); + await_(sleep2(3500).future); } }); @@ -52,7 +52,7 @@ int main() { async_ ([](){ loop { cout << "Loop 4" << endl; - sleep(4000); + await_(sleep2(4000).future); } }); @@ -60,7 +60,7 @@ int main() { async_ ([](){ loop { cout << "Loop 45" << endl; - sleep(4500); + await_(sleep2(4500).future); } }); @@ -68,7 +68,7 @@ int main() { async_ ([](){ loop { cout << "Loop 5" << endl; - sleep(5000); + await_(sleep2(5000).future); } }); @@ -76,34 +76,34 @@ int main() { async_ ([](){ loop { cout << "Loop 55" << endl; - sleep(5500); + await_(sleep2(5500).future); } }); async_ ([](){ loop { cout << "Loop 6" << endl; - sleep(6000); + await_(sleep2(6000).future); } }); async_ ([](){ loop { cout << "Loop 65" << endl; - sleep(6500); + await_(sleep2(6500).future); } }); async_ ([](){ loop { cout << "Loop 7" << endl; - sleep(7000); + await_(sleep2(7000).future); } }); loop { // blokira trenutnu cout << "Loop 15" << endl; - sleep(1500); + sleep2(1500).future.get(); } asynco_default_join();