diff --git a/lib/asynco.hpp b/lib/asynco.hpp index 3fac408..eed2375 100644 --- a/lib/asynco.hpp +++ b/lib/asynco.hpp @@ -53,26 +53,10 @@ std::future async_(boost::asio::awaitable _coroutine) { #endif /** - * Block until the asynchronous call completes + * Block until the asynchronous call completes - dont block asynco engine loop */ template -T await_(future& r) { - return r.get(); -} - -/** - * Block until the asynchronous call completes -*/ -template -T await_(future&& r) { - return move(r).get(); -} - -/** - * Block until the asynchronous call completes -*/ -template -T await_(future& r, uint32_t time_us = 10) { +T await_(future& r, uint16_t time_us = 10) { while (r.wait_for(std::chrono::microseconds(time_us)) != std::future_status::ready) { _asynco_engine.io_context.poll_one(); } @@ -80,10 +64,10 @@ T await_(future& r, uint32_t time_us = 10) { } /** - * Block until the asynchronous call completes + * Block until the asynchronous call completes - dont block asynco engine loop */ template -T await_(future&& r, uint32_t time_us = 10) { +T await_(future&& r, uint16_t time_us = 10) { while (r.wait_for(std::chrono::microseconds(time_us)) != std::future_status::ready) { _asynco_engine.io_context.poll_one(); } @@ -116,29 +100,6 @@ T await_(boost::asio::awaitable _coroutine) { #endif - -/** - * Block until the asynchronous call completes or time expired -*/ -// template -// T await_(future& r, uint64_t time) { -// if (r.wait_for(chrono::milliseconds(time)) == std::future_status::timeout) { -// throw runtime_error("Asynchronous execution timed out"); -// } -// return r.get(); -// } - -/** - * Block until the asynchronous call completes or time expired -*/ -// template -// T await_(future&& r, uint64_t time) { -// if (r.wait_for(chrono::milliseconds(time)) == std::future_status::timeout) { -// throw runtime_error("Asynchronous execution timed out"); -// } -// return move(r).get(); -// } - } } diff --git a/test/main.cpp b/test/main.cpp index 0088a6e..839d367 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -45,6 +45,25 @@ using namespace this_thread; // return _promise.get_future().get(); // } +// void sleep_to (int _time) { +// promise _promise; +// Delayed t( [&]() { +// _promise.set_value(); +// }, _time); +// await_ (_promise.get_future(), 100); +// } + +// future sleep_to (int _time) { +// promise _promise; +// future _future = _promise.get_future(); + +// Delayed t( [&]() { +// _promise.set_value(); +// }, _time); + +// return _future; +// } + // void promise_reject (int _time) { // promise _promise; // Delayed t( [&]() { @@ -71,14 +90,14 @@ using namespace this_thread; // } // }; -// // ------------------ EXTEND OWN CLASS WITH EVENTS ------------------- +// // // ------------------ EXTEND OWN CLASS WITH EVENTS ------------------- // class myOwnClass : public Trigger { // public: // myOwnClass() : Trigger() {}; // }; -// ----------------- MULTIPLE TRIGGERS IN ONE CLASS ------------------ +// // ----------------- MULTIPLE TRIGGERS IN ONE CLASS ------------------ // class ClassWithTriggers { // Trigger emitter1; @@ -176,25 +195,6 @@ int main () { // cout << "nije isteko " << endl; // } - // auto d = Delayed( [](){ - // cout << "Delayed" << endl; - // }, 2000); - - // auto p = Periodic( [](){ - // cout << "Periodic" << endl; - // }, 700); - - // Periodic( [&] (){ - // cout << "Delayed expire " << d->expired() << endl; - // cout << "Periodic ticks " << p->ticks() << endl; - // cout << "Delayed stoped " << d->stoped() << endl; - // cout << "Periodic stoped " << p->stoped() << endl; - // }, 1000); - - // Delayed( [&](){ - // p->stop(); - // }, 10000); - // // // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS ------------------------- // // /** @@ -460,7 +460,7 @@ int main () { // }); - // // ---------------------------------------------------------------------------------------------------- + // ---------------------------------------------------------------------------------------------------- // auto i = async_ ( []() -> asyncable { @@ -481,7 +481,7 @@ int main () { // }, 2000); - // Periodic b( []() { + // Periodic b_( []() { // cout << "funckija" << endl; // }, 2000); @@ -565,7 +565,7 @@ int main () { // ); // }, 2000); - // await_( async_co2 ( [c1 = move(c1)]() -> asyncable { + // await_( async_ ( [c1 = move(c1)]() -> asyncable { // cout << "Baba roga" << endl; // co_await c1(); // })); @@ -578,7 +578,32 @@ int main () { // evoid.tick("void"); - cout << "-------------end main-------------" << endl; + + + + // vector> futures; + + // for (int i=0; i<20; i++) { + // futures.push_back( + // async_([a = i](){ + // for (int i=0; i<1000; i++) { + // cout << a << " " << i << endl; + // // sleep_to(i); + // } + // }) + // ); + // } + + // for (int i=0; i<20; i++) { + // await_(futures[i]); + // // await_(futures[i]); + // } + + + + + + cout << "-------------end main------------- " << rtime_ms() - start << endl; _asynco_engine.run(); return 0; }