From a1606fd3b66907d5209932f693041b7a3c470fd1 Mon Sep 17 00:00:00 2001 From: mbandic Date: Mon, 31 Mar 2025 12:22:38 +0200 Subject: [PATCH] Await all on no-void return values --- lib/asynco.hpp | 20 ++++++++++++++++++++ test/main.cpp | 45 ++++++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/lib/asynco.hpp b/lib/asynco.hpp index f06a492..6358cb0 100644 --- a/lib/asynco.hpp +++ b/lib/asynco.hpp @@ -35,6 +35,26 @@ T await_(future&& r) { return move(r).get(); } +/** + * Block until the multiple asynchronous call completes + * Use only on no-void calls + */ + +template +auto await_(F&&... f) -> std::tuple::type...> { + return std::make_tuple(move(f).get()...); +} + +/** + * Block until the multiple asynchronous call completes + * Use only on no-void calls + */ + +template +auto await_(F&... f) -> std::tuple::type...> { + return std::make_tuple(f.get()...); +} + /** * Block until the asynchronous call completes or time expired */ diff --git a/test/main.cpp b/test/main.cpp index 98defce..18675c7 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -278,24 +278,39 @@ int main () { // // -------------------------- AWAIT ALL ---------------------------------- - // auto a = async_ ( []() { - // cout << "A" << endl; - // return 3; - // }); + auto a = async_ ( []() { + cout << "A" << endl; + return 3; + }); - // auto b = async_ ( []() { - // cout << "B" << endl; - // throw runtime_error("Test exception"); - // return; - // }); + auto b = async_ ( []() { + cout << "B" << endl; + // throw runtime_error("Test exception"); + return; + }); - // auto c = async_ ( []() { - // cout << "C" << endl; - // return "Hello"; - // }); + auto c = async_ ( []() { + cout << "C" << endl; + return "Hello"; + }); - // int a_; - // string c_; + int a_; + string c_; + + // auto all = await_(a, c); + // cout << get<0>(all) << get<1>(all) << endl; + + // ili + + tie(a_, c_) = await_(a, c); + cout << a_ << c_ << endl; + + int d_; + float e_; + tie(d_, e_) = await_( async_ ( []() {return 1;}), async_ ([](){ return 4.3;})); + + cout << d_ << e_ << endl; + // auto await_all = [&] () { // a_ = await_(a);