From 28763725520e3803bb51a6086994b34b048751bf Mon Sep 17 00:00:00 2001 From: mbandic Date: Thu, 26 Sep 2024 07:23:33 +0000 Subject: [PATCH] Add example for await all --- README.md | 54 +++++++++++++++++++++++++++++ test/test.cpp | 96 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 129 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 6a69817..3bbf82f 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,60 @@ cout << await_(async_ ( [] () { return 4; })) << endl; + +/** + * Await all + **/ + + auto a = async_ ( []() { + cout << "A" << endl; + return 3; + }); + + auto b = async_ ( []() { + cout << "B" << endl; + throw runtime_error("Test exception"); + return; + }); + + auto c = async_ ( []() { + cout << "C" << endl; + return "Hello"; + }); + + int a_; + string c_; + + auto await_all = [&] () { + a_ = await_(a); + await_(b); + c_ = await_(c); + }; + + try { + await_all(); + cout << "a_ " << a_ << " c_ " << c_ << endl; + } catch (const exception& exc) { + cout << exc.what() << endl; + } + + // // same type + + vector> fut_vec; + for (int i=0; i<5; i++) { + fut_vec.push_back( + async_ ( [i]() { + cout << "Async_ " << i << endl; + }) + ); + } + + auto await_all = [&] () { + for (int i=0; i #include #include +#include using namespace std; using namespace this_thread; @@ -134,24 +135,25 @@ int main () { // * Run an function asyncronic // */ - async_ ( []() { - sleep_for(2s); // only for simulate log duration function - cout << "asynco 1" << endl; - return 5; - }); + // async_ ( []() { + // sleep_for(2s); // only for simulate log duration function + // cout << "asynco 1" << endl; + // return 5; + // }); - /** - * Call not lambda function - */ + // /** + // * Call not lambda function + // */ - async_ (notLambdaFunction); + // async_ (notLambdaFunction); - await_ ( - async_ ( - notLambdaFunction - ) - ); + // await_ ( + // async_ ( + // notLambdaFunction + // ) + // ); + // async(launch::async, [] () { // cout << "Another thread in async style!" << endl; @@ -162,7 +164,7 @@ int main () { // */ // clm classes; - // asynco( [&classes] () { + // async_ ( [&classes] () { // classes.classMethode(); // }); @@ -172,20 +174,20 @@ int main () { // * await_ after runned as async // */ - // auto a = asynco( []() { + // auto a = async_ ( []() { // sleep_for(2s); // only for simulate log duration function - // cout << "asynco 2" << endl; + // cout << "async_ 2" << endl; // return 5; // }); // cout << await_(a) << endl; - // cout << "print after asynco 2" << endl; + // cout << "print after async_ 2" << endl; // /** // * await_ async function call and use i cout // */ - // cout << await_(asynco( [] () { + // cout << await_(async_ ( [] () { // sleep_for(chrono::seconds(1)); // only for simulate log duration function // cout << "await_ end" << endl; // return 4; @@ -216,13 +218,65 @@ int main () { // */ - // asynco( [] { + // async_ ( [] { // cout << "idemo ..." << endl; - // asynco( [] { + // async_ ( [] { // cout << "ugdnježdena async funkcija " << endl; // }); // }); + + // // -------------------------- AWAIT ALL ---------------------------------- + + // auto a = async_ ( []() { + // cout << "A" << endl; + // return 3; + // }); + + // auto b = async_ ( []() { + // cout << "B" << endl; + // throw runtime_error("Test exception"); + // return; + // }); + + // auto c = async_ ( []() { + // cout << "C" << endl; + // return "Hello"; + // }); + + // int a_; + // string c_; + + // auto await_all = [&] () { + // a_ = await_(a); + // await_(b); + // c_ = await_(c); + // }; + + // try { + // await_all(); + // cout << "a_ " << a_ << " c_ " << c_ << endl; + // } catch (const exception& exc) { + // cout << exc.what() << endl; + // } + + // // // same type + + // vector> fut_vec; + // for (int i=0; i<5; i++) { + // fut_vec.push_back( + // async_ ( [i]() { + // cout << "Async_ " << i << endl; + // }) + // ); + // } + + // auto await_all = [&] () { + // for (int i=0; i