Work on sleep2 with future

This commit is contained in:
marcelb 2025-09-29 14:50:29 +02:00
parent 4283f32826
commit c8f6aa877c
6 changed files with 114 additions and 89 deletions

View File

@ -26,6 +26,12 @@ using namespace boost::asio;
namespace marcelb { namespace marcelb {
namespace asynco { namespace asynco {
struct SleepHandle {
std::future<void> future;
std::shared_ptr<Timer> timer;
};
/** /**
* Asynco runtime * Asynco runtime
* Used for all asynchronous capabilities of this wrapper * Used for all asynchronous capabilities of this wrapper
@ -178,6 +184,9 @@ public:
void sleep(int _time); void sleep(int _time);
SleepHandle sleep2(int _time);
/** /**
* Initialize trigger (typed event) * Initialize trigger (typed event)
*/ */

View File

@ -102,6 +102,7 @@ Timer periodic(function<void()> callback, uint64_t time);
*/ */
void sleep(int _time); void sleep(int _time);
SleepHandle sleep2(int _time);
/** /**
* Initialize trigger (typed event) * Initialize trigger (typed event)

View File

@ -46,4 +46,14 @@ void Asynco::sleep(int _time) {
return await(_promise.get_future()); return await(_promise.get_future());
} }
SleepHandle Asynco::sleep2(int _time) {
auto _promise = std::make_shared<std::promise<void>>();
auto _timer = std::make_shared<Timer>(io_ctx, [_promise]() {
_promise->set_value();
}, _time, TimerType::Delayed);
return { _promise->get_future(), _timer };
}
}; };

View File

@ -17,6 +17,11 @@ void sleep(int _time) {
return Asynco_Default_Runtime.sleep(_time); return Asynco_Default_Runtime.sleep(_time);
} }
SleepHandle sleep2(int _time) {
return Asynco_Default_Runtime.sleep2(_time);
}
Asynco& asynco_default_runtime() { Asynco& asynco_default_runtime() {
return Asynco_Default_Runtime; return Asynco_Default_Runtime;
} }

View File

@ -9,102 +9,102 @@ int main() {
Asynco asynco; Asynco asynco;
asynco.run(4); asynco.run(4);
asynco.async ([&](){ // asynco.async ([&](){
loop { // loop {
cout << "Loop 1" << endl; // cout << "Loop 1" << endl;
asynco.sleep(1000); // asynco.sleep(1000);
} // }
}); // });
asynco.async ([&](){ // asynco.async ([&](){
loop { // loop {
cout << "Loop 2" << endl; // cout << "Loop 2" << endl;
asynco.sleep(2000); // asynco.sleep(2000);
} // }
}); // });
asynco.async ([&](){ // asynco.async ([&](){
loop { // loop {
cout << "Loop 25" << endl; // cout << "Loop 25" << endl;
asynco.sleep(2500); // asynco.sleep(2500);
} // }
}); // });
asynco.async ([&](){ // asynco.async ([&](){
loop { // loop {
cout << "Loop 3" << endl; // cout << "Loop 3" << endl;
asynco.sleep(3000); // asynco.sleep(3000);
} // }
}); // });
asynco.async ([&](){ // asynco.async ([&](){
loop { // loop {
cout << "Loop 35" << endl; // cout << "Loop 35" << endl;
asynco.sleep(3500); // asynco.sleep(3500);
} // }
}); // });
asynco.async ([&](){ // asynco.async ([&](){
loop { // loop {
cout << "Loop 4" << endl; // cout << "Loop 4" << endl;
asynco.sleep(4000); // asynco.sleep(4000);
} // }
}); // });
asynco.async ([&](){ // asynco.async ([&](){
loop { // loop {
cout << "Loop 45" << endl; // cout << "Loop 45" << endl;
asynco.sleep(4500); // asynco.sleep(4500);
} // }
}); // });
asynco.async ([&](){ // asynco.async ([&](){
loop { // loop {
cout << "Loop 5" << endl; // cout << "Loop 5" << endl;
asynco.sleep(5000); // asynco.sleep(5000);
} // }
}); // });
asynco.async ([&](){ // asynco.async ([&](){
loop { // loop {
cout << "Loop 55" << endl; // cout << "Loop 55" << endl;
asynco.sleep(5500); // asynco.sleep(5500);
} // }
}); // });
asynco.async ([&](){ // asynco.async ([&](){
loop { // loop {
cout << "Loop 6" << endl; // cout << "Loop 6" << endl;
asynco.sleep(6000); // asynco.sleep(6000);
} // }
}); // });
asynco.async ([&](){ // asynco.async ([&](){
loop { // loop {
cout << "Loop 65" << endl; // cout << "Loop 65" << endl;
asynco.sleep(6500); // asynco.sleep(6500);
} // }
}); // });
asynco.async ([&](){ // asynco.async ([&](){
loop { // loop {
cout << "Loop 7" << endl; // cout << "Loop 7" << endl;
asynco.sleep(7000); // asynco.sleep(7000);
} // }
}); // });
loop { // blokira trenutnu // loop { // blokira trenutnu
cout << "Loop 15" << endl; // cout << "Loop 15" << endl;
asynco.sleep(1500); // asynco.sleep(1500);
} // }
asynco.join(); asynco.join();
return 0; return 0;

View File

@ -12,7 +12,7 @@ int main() {
async_ ([](){ async_ ([](){
loop { loop {
cout << "Loop 1" << endl; cout << "Loop 1" << endl;
sleep(1000); await_(sleep2(1000).future);
} }
}); });
@ -20,7 +20,7 @@ int main() {
async_ ([](){ async_ ([](){
loop { loop {
cout << "Loop 2" << endl; cout << "Loop 2" << endl;
sleep(2000); await_(sleep2(2000).future);
} }
}); });
@ -28,7 +28,7 @@ int main() {
async_ ([](){ async_ ([](){
loop { loop {
cout << "Loop 25" << endl; cout << "Loop 25" << endl;
sleep(2500); await_(sleep2(2500).future);
} }
}); });
@ -36,7 +36,7 @@ int main() {
async_ ([](){ async_ ([](){
loop { loop {
cout << "Loop 3" << endl; cout << "Loop 3" << endl;
sleep(3000); await_(sleep2(3000).future);
} }
}); });
@ -44,7 +44,7 @@ int main() {
async_ ([](){ async_ ([](){
loop { loop {
cout << "Loop 35" << endl; cout << "Loop 35" << endl;
sleep(3500); await_(sleep2(3500).future);
} }
}); });
@ -52,7 +52,7 @@ int main() {
async_ ([](){ async_ ([](){
loop { loop {
cout << "Loop 4" << endl; cout << "Loop 4" << endl;
sleep(4000); await_(sleep2(4000).future);
} }
}); });
@ -60,7 +60,7 @@ int main() {
async_ ([](){ async_ ([](){
loop { loop {
cout << "Loop 45" << endl; cout << "Loop 45" << endl;
sleep(4500); await_(sleep2(4500).future);
} }
}); });
@ -68,7 +68,7 @@ int main() {
async_ ([](){ async_ ([](){
loop { loop {
cout << "Loop 5" << endl; cout << "Loop 5" << endl;
sleep(5000); await_(sleep2(5000).future);
} }
}); });
@ -76,34 +76,34 @@ int main() {
async_ ([](){ async_ ([](){
loop { loop {
cout << "Loop 55" << endl; cout << "Loop 55" << endl;
sleep(5500); await_(sleep2(5500).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
cout << "Loop 6" << endl; cout << "Loop 6" << endl;
sleep(6000); await_(sleep2(6000).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
cout << "Loop 65" << endl; cout << "Loop 65" << endl;
sleep(6500); await_(sleep2(6500).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
cout << "Loop 7" << endl; cout << "Loop 7" << endl;
sleep(7000); await_(sleep2(7000).future);
} }
}); });
loop { // blokira trenutnu loop { // blokira trenutnu
cout << "Loop 15" << endl; cout << "Loop 15" << endl;
sleep(1500); sleep2(1500).future.get();
} }
asynco_default_join(); asynco_default_join();