From 7bf7a7d090dbb93c6862ff52ffdc3547e2d5244c Mon Sep 17 00:00:00 2001 From: mbandic Date: Thu, 26 Sep 2024 06:51:45 +0000 Subject: [PATCH] Async_ await_ --- README.md | 24 ++++++++++++------------ lib/asynco.hpp | 7 +++---- lib/define.hpp | 4 ++-- lib/filesystem.hpp | 8 ++++---- lib/trigger.hpp | 2 +- test/test.cpp | 20 ++++++++++---------- 6 files changed, 32 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 8f9683f..6a69817 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Just download the latest release and unzip it into your project. ```c++ #define NUM_OF_RUNNERS 8 // To change the number of threads used by asynco, without this it runs according to the number of cores -#include "asynco/lib/asynco.hpp" // asynco(), wait() +#include "asynco/lib/asynco.hpp" // async_ (), await_() #include "asynco/lib/triggers.hpp" // trigger (event emitter) #include "asynco/lib/timers.hpp" // periodic, delayed (like setInterval and setTimeout from JS) #include "asynco/lib/filesystem.hpp" // for async read and write files @@ -85,7 +85,7 @@ Make functions asynchronous * Run an lambda function asynchronously */ -asynco( []() { +async_ ( []() { sleep_for(2s); // only for simulating long duration function cout << "asynco" << endl; return 5; @@ -100,7 +100,7 @@ void notLambdaFunction() { cout << "Call to not lambda function" << endl; } -asynco (notLambdaFunction); +async_ (notLambdaFunction); /** * Run class method @@ -114,31 +114,31 @@ class clm { }; clm classes; -asynco( [&classes] () { +async_ ( [&classes] () { classes.classMethode(); }); /** -* Wait after runned as async +* await_ after runned as async */ -auto a = asynco( []() { +auto a = async_ ( []() { sleep_for(2s); // only for simulating long duration function cout << "asynco" << endl; return 5; }); -cout << wait(a) << endl; +cout << await_(a) << endl; /** -* Wait async function call and use i cout +* await_ async function call and use i cout */ -cout << wait(asynco( [] () { +cout << await_(async_ ( [] () { sleep_for(chrono::seconds(1)); // only for simulating long duration function - cout << "wait end" << endl; + cout << "await_ end" << endl; return 4; })) << endl; @@ -279,7 +279,7 @@ fs::write("test1.txt", "Hello world", [] (exception* error) { auto future_data = fs::read("test.txt"); try { - string data = wait(future_data); + string data = await_(future_data); } catch (exception& err) { cout << err.what() << endl; } @@ -287,7 +287,7 @@ try { auto future_status = fs::write("test.txt", "Hello world"); try { - wait(future_status); + await_(future_status); } catch (exception& err) { cout << err.what() << endl; } diff --git a/lib/asynco.hpp b/lib/asynco.hpp index 99023d0..2455f0f 100644 --- a/lib/asynco.hpp +++ b/lib/asynco.hpp @@ -11,7 +11,6 @@ namespace asynco { #define HW_CONCURRENCY_MINIMAL 4 - /** * Internal anonymous class for initializing the ASIO context and thread pool * !!! It is anonymous to protect against use in the initialization of other objects of the same type !!! @@ -59,7 +58,7 @@ class { * Run the function asynchronously */ template -auto nonsync(F&& f, Args&&... args) -> future::type> { +auto async_(F&& f, Args&&... args) -> future::type> { using return_type = typename result_of::type; future res = _asynco_engine.io_context.post(boost::asio::use_future(bind(forward(f), forward(args)...))); return res; @@ -69,7 +68,7 @@ auto nonsync(F&& f, Args&&... args) -> future::ty * Block until the asynchronous call completes */ template -T wait(future& r) { +T await_(future& r) { return r.get(); } @@ -77,7 +76,7 @@ T wait(future& r) { * Block until the asynchronous call completes */ template -T wait(future&& r) { +T await_(future&& r) { return move(r).get(); } diff --git a/lib/define.hpp b/lib/define.hpp index e33b870..bf2629a 100644 --- a/lib/define.hpp +++ b/lib/define.hpp @@ -8,8 +8,8 @@ namespace asynco { * Alternative names of functions - mostly for the sake of more beautiful coloring of the code */ -#define nonsync marcelb::asynco::nonsync -#define wait marcelb::asynco::wait +#define async_ marcelb::asynco::async_ +#define await_ marcelb::asynco::await_ } } diff --git a/lib/filesystem.hpp b/lib/filesystem.hpp index 33f01dc..b35de20 100644 --- a/lib/filesystem.hpp +++ b/lib/filesystem.hpp @@ -19,7 +19,7 @@ namespace fs { */ template void read(string path, Callback&& callback) { - asynco::nonsync( [&path, callback] () { + asynco::async_( [&path, callback] () { string content; try { string line; @@ -48,7 +48,7 @@ void read(string path, Callback&& callback) { * Asynchronous file reading */ future read(string path) { - return asynco::nonsync( [&path] () { + return asynco::async_( [&path] () { string content; string line; ifstream file (path); @@ -72,7 +72,7 @@ future read(string path) { */ template void write(string path, string content, Callback&& callback) { - asynco::nonsync( [&path, &content, callback] () { + asynco::async_( [&path, &content, callback] () { try { ofstream file (path); if (file.is_open()) { @@ -95,7 +95,7 @@ void write(string path, string content, Callback&& callback) { * Asynchronous file writing with callback after write complete */ future write(string path, string content) { - return asynco::nonsync( [&path, &content] () { + return asynco::async_( [&path, &content] () { ofstream file (path); if (file.is_open()) { file << content; diff --git a/lib/trigger.hpp b/lib/trigger.hpp index 4361875..f4abacc 100644 --- a/lib/trigger.hpp +++ b/lib/trigger.hpp @@ -42,7 +42,7 @@ class trigger { if (it_eve != triggers.end()) { for (uint i =0; isecond.size(); i++) { auto callback = bind(it_eve->second[i], forward(args)...); - asynco::nonsync(callback); + asynco::async_(callback); } } } diff --git a/test/test.cpp b/test/test.cpp index 35bb0d7..6f38663 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -134,7 +134,7 @@ int main () { // * Run an function asyncronic // */ - nonsync ( []() { + async_ ( []() { sleep_for(2s); // only for simulate log duration function cout << "asynco 1" << endl; return 5; @@ -144,11 +144,11 @@ int main () { * Call not lambda function */ - nonsync (notLambdaFunction); + async_ (notLambdaFunction); - wait ( - nonsync ( + await_ ( + async_ ( notLambdaFunction ) ); @@ -169,7 +169,7 @@ int main () { // sleep(5); // /** - // * Wait after runned as async + // * await_ after runned as async // */ // auto a = asynco( []() { @@ -178,16 +178,16 @@ int main () { // return 5; // }); - // cout << wait(a) << endl; + // cout << await_(a) << endl; // cout << "print after asynco 2" << endl; // /** - // * Wait async function call and use i cout + // * await_ async function call and use i cout // */ - // cout << wait(asynco( [] () { + // cout << await_(asynco( [] () { // sleep_for(chrono::seconds(1)); // only for simulate log duration function - // cout << "wait end" << endl; + // cout << "await_ end" << endl; // return 4; // })) << endl; @@ -298,7 +298,7 @@ int main () { // try { - // auto data = wait(status); + // auto data = await_(status); // cout << data; // } catch (exception& err) { // cout << err.what() << endl;