Compare commits

..

No commits in common. '8b23bd67288a643d8be5f2574328a2e8837d6d0c' and 'd8e0d0b49d70b1e1e9453d81245f33a811f962e5' have entirely different histories.

  1. 78
      README.md
  2. 7
      lib/asynco.hpp
  3. 4
      lib/define.hpp
  4. 8
      lib/filesystem.hpp
  5. 2
      lib/trigger.hpp
  6. 106
      test/test.cpp

@ -29,7 +29,7 @@ Just download the latest release and unzip it into your project.
```c++ ```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 #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" // async_ (), await_() #include "asynco/lib/asynco.hpp" // asynco(), wait()
#include "asynco/lib/triggers.hpp" // trigger (event emitter) #include "asynco/lib/triggers.hpp" // trigger (event emitter)
#include "asynco/lib/timers.hpp" // periodic, delayed (like setInterval and setTimeout from JS) #include "asynco/lib/timers.hpp" // periodic, delayed (like setInterval and setTimeout from JS)
#include "asynco/lib/filesystem.hpp" // for async read and write files #include "asynco/lib/filesystem.hpp" // for async read and write files
@ -85,7 +85,7 @@ Make functions asynchronous
* Run an lambda function asynchronously * Run an lambda function asynchronously
*/ */
async_ ( []() { nonsync ( []() {
sleep_for(2s); // only for simulating long duration function sleep_for(2s); // only for simulating long duration function
cout << "nonsync " << endl; cout << "nonsync " << endl;
return 5; return 5;
@ -100,7 +100,7 @@ void notLambdaFunction() {
cout << "Call to not lambda function" << endl; cout << "Call to not lambda function" << endl;
} }
async_ (notLambdaFunction); nonsync (notLambdaFunction);
/** /**
* Run class method * Run class method
@ -114,88 +114,34 @@ class clm {
}; };
clm classes; clm classes;
async_ ( [&classes] () { nonsync ( [&classes] () {
classes.classMethode(); classes.classMethode();
}); });
/** /**
* await_ after runned as async * Wait after runned as async
*/ */
auto a = async_ ( []() { auto a = nonsync ( []() {
sleep_for(2s); // only for simulating long duration function sleep_for(2s); // only for simulating long duration function
cout << "nonsync " << endl; cout << "nonsync " << endl;
return 5; return 5;
}); });
cout << await_(a) << endl; cout << wait(a) << endl;
/** /**
* await_ async function call and use i cout * Wait async function call and use i cout
*/ */
cout << await_(async_ ( [] () { cout << wait(nonsync ( [] () {
sleep_for(chrono::seconds(1)); // only for simulating long duration function sleep_for(chrono::seconds(1)); // only for simulating long duration function
cout << "await_ end" << endl; cout << "wait end" << endl;
return 4; return 4;
})) << endl; })) << 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<future<void>> 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<fut_vec.size(); i++) {
await_ (fut_vec[i]);
}
};
/** /**
* Sleep with delayed sleep implement * Sleep with delayed sleep implement
*/ */
@ -333,7 +279,7 @@ fs::write("test1.txt", "Hello world", [] (exception* error) {
auto future_data = fs::read("test.txt"); auto future_data = fs::read("test.txt");
try { try {
string data = await_(future_data); string data = wait(future_data);
} catch (exception& err) { } catch (exception& err) {
cout << err.what() << endl; cout << err.what() << endl;
} }
@ -341,7 +287,7 @@ try {
auto future_status = fs::write("test.txt", "Hello world"); auto future_status = fs::write("test.txt", "Hello world");
try { try {
await_(future_status); wait(future_status);
} catch (exception& err) { } catch (exception& err) {
cout << err.what() << endl; cout << err.what() << endl;
} }

@ -11,6 +11,7 @@ namespace asynco {
#define HW_CONCURRENCY_MINIMAL 4 #define HW_CONCURRENCY_MINIMAL 4
/** /**
* Internal anonymous class for initializing the ASIO context and thread pool * 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 !!! * !!! It is anonymous to protect against use in the initialization of other objects of the same type !!!
@ -58,7 +59,7 @@ class {
* Run the function asynchronously * Run the function asynchronously
*/ */
template<class F, class... Args> template<class F, class... Args>
auto async_(F&& f, Args&&... args) -> future<typename result_of<F(Args...)>::type> { auto nonsync(F&& f, Args&&... args) -> future<typename result_of<F(Args...)>::type> {
using return_type = typename result_of<F(Args...)>::type; using return_type = typename result_of<F(Args...)>::type;
future<return_type> res = _asynco_engine.io_context.post(boost::asio::use_future(bind(forward<F>(f), forward<Args>(args)...))); future<return_type> res = _asynco_engine.io_context.post(boost::asio::use_future(bind(forward<F>(f), forward<Args>(args)...)));
return res; return res;
@ -68,7 +69,7 @@ auto async_(F&& f, Args&&... args) -> future<typename result_of<F(Args...)>::typ
* Block until the asynchronous call completes * Block until the asynchronous call completes
*/ */
template<typename T> template<typename T>
T await_(future<T>& r) { T wait(future<T>& r) {
return r.get(); return r.get();
} }
@ -76,7 +77,7 @@ T await_(future<T>& r) {
* Block until the asynchronous call completes * Block until the asynchronous call completes
*/ */
template<typename T> template<typename T>
T await_(future<T>&& r) { T wait(future<T>&& r) {
return move(r).get(); return move(r).get();
} }

@ -8,8 +8,8 @@ namespace asynco {
* Alternative names of functions - mostly for the sake of more beautiful coloring of the code * Alternative names of functions - mostly for the sake of more beautiful coloring of the code
*/ */
#define async_ marcelb::asynco::async_ #define nonsync marcelb::asynco::nonsync
#define await_ marcelb::asynco::await_ #define wait marcelb::asynco::wait
} }
} }

@ -19,7 +19,7 @@ namespace fs {
*/ */
template<typename Callback> template<typename Callback>
void read(string path, Callback&& callback) { void read(string path, Callback&& callback) {
asynco::async_( [&path, callback] () { asynco::nonsync( [&path, callback] () {
string content; string content;
try { try {
string line; string line;
@ -48,7 +48,7 @@ void read(string path, Callback&& callback) {
* Asynchronous file reading * Asynchronous file reading
*/ */
future<string> read(string path) { future<string> read(string path) {
return asynco::async_( [&path] () { return asynco::nonsync( [&path] () {
string content; string content;
string line; string line;
ifstream file (path); ifstream file (path);
@ -72,7 +72,7 @@ future<string> read(string path) {
*/ */
template<typename Callback> template<typename Callback>
void write(string path, string content, Callback&& callback) { void write(string path, string content, Callback&& callback) {
asynco::async_( [&path, &content, callback] () { asynco::nonsync( [&path, &content, callback] () {
try { try {
ofstream file (path); ofstream file (path);
if (file.is_open()) { if (file.is_open()) {
@ -95,7 +95,7 @@ void write(string path, string content, Callback&& callback) {
* Asynchronous file writing with callback after write complete * Asynchronous file writing with callback after write complete
*/ */
future<void> write(string path, string content) { future<void> write(string path, string content) {
return asynco::async_( [&path, &content] () { return asynco::nonsync( [&path, &content] () {
ofstream file (path); ofstream file (path);
if (file.is_open()) { if (file.is_open()) {
file << content; file << content;

@ -42,7 +42,7 @@ class trigger {
if (it_eve != triggers.end()) { if (it_eve != triggers.end()) {
for (uint i =0; i<it_eve->second.size(); i++) { for (uint i =0; i<it_eve->second.size(); i++) {
auto callback = bind(it_eve->second[i], forward<Args>(args)...); auto callback = bind(it_eve->second[i], forward<Args>(args)...);
asynco::async_(callback); asynco::nonsync(callback);
} }
} }
} }

@ -13,7 +13,6 @@ using namespace triggers;
#include <unistd.h> #include <unistd.h>
#include <thread> #include <thread>
#include <future> #include <future>
#include <vector>
using namespace std; using namespace std;
using namespace this_thread; using namespace this_thread;
@ -135,25 +134,24 @@ int main () {
// * Run an function asyncronic // * Run an function asyncronic
// */ // */
// async_ ( []() { nonsync ( []() {
// sleep_for(2s); // only for simulate log duration function sleep_for(2s); // only for simulate log duration function
// cout << "asynco 1" << endl; cout << "asynco 1" << endl;
// return 5; return 5;
// }); });
// /** /**
// * Call not lambda function * Call not lambda function
// */ */
// async_ (notLambdaFunction);
nonsync (notLambdaFunction);
// await_ (
// async_ (
// notLambdaFunction
// )
// );
wait (
nonsync (
notLambdaFunction
)
);
// async(launch::async, [] () { // async(launch::async, [] () {
// cout << "Another thread in async style!" << endl; // cout << "Another thread in async style!" << endl;
@ -164,32 +162,32 @@ int main () {
// */ // */
// clm classes; // clm classes;
// async_ ( [&classes] () { // asynco( [&classes] () {
// classes.classMethode(); // classes.classMethode();
// }); // });
// sleep(5); // sleep(5);
// /** // /**
// * await_ after runned as async // * Wait after runned as async
// */ // */
// auto a = async_ ( []() { // auto a = asynco( []() {
// sleep_for(2s); // only for simulate log duration function // sleep_for(2s); // only for simulate log duration function
// cout << "async_ 2" << endl; // cout << "asynco 2" << endl;
// return 5; // return 5;
// }); // });
// cout << await_(a) << endl; // cout << wait(a) << endl;
// cout << "print after async_ 2" << endl; // cout << "print after asynco 2" << endl;
// /** // /**
// * await_ async function call and use i cout // * Wait async function call and use i cout
// */ // */
// cout << await_(async_ ( [] () { // cout << wait(asynco( [] () {
// sleep_for(chrono::seconds(1)); // only for simulate log duration function // sleep_for(chrono::seconds(1)); // only for simulate log duration function
// cout << "await_ end" << endl; // cout << "wait end" << endl;
// return 4; // return 4;
// })) << endl; // })) << endl;
@ -218,65 +216,13 @@ int main () {
// */ // */
// async_ ( [] { // asynco( [] {
// cout << "idemo ..." << endl; // cout << "idemo ..." << endl;
// async_ ( [] { // asynco( [] {
// cout << "ugdnježdena async funkcija " << endl; // 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<future<void>> 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<fut_vec.size(); i++) {
// await_ (fut_vec[i]);
// }
// };
// --------------- EVENTS ------------------- // --------------- EVENTS -------------------
/** /**
@ -352,7 +298,7 @@ int main () {
// try { // try {
// auto data = await_(status); // auto data = wait(status);
// cout << data; // cout << data;
// } catch (exception& err) { // } catch (exception& err) {
// cout << err.what() << endl; // cout << err.what() << endl;

Loading…
Cancel
Save