|
|
@ -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" // asynco(), wait() |
|
|
|
#include "asynco/lib/asynco.hpp" // async_ (), await_() |
|
|
|
#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 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
nonsync ( []() { |
|
|
|
async_ ( []() { |
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
nonsync (notLambdaFunction); |
|
|
|
async_ (notLambdaFunction); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Run class method |
|
|
|
* Run class method |
|
|
@ -114,34 +114,88 @@ class clm { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
clm classes; |
|
|
|
clm classes; |
|
|
|
nonsync ( [&classes] () { |
|
|
|
async_ ( [&classes] () { |
|
|
|
classes.classMethode(); |
|
|
|
classes.classMethode(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Wait after runned as async |
|
|
|
* await_ after runned as async |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
auto a = nonsync ( []() { |
|
|
|
auto a = async_ ( []() { |
|
|
|
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 << 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(nonsync ( [] () { |
|
|
|
cout << await_(async_ ( [] () { |
|
|
|
sleep_for(chrono::seconds(1)); // only for simulating long duration function |
|
|
|
sleep_for(chrono::seconds(1)); // only for simulating long duration function |
|
|
|
cout << "wait end" << endl; |
|
|
|
cout << "await_ 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 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -279,7 +333,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 = wait(future_data); |
|
|
|
string data = await_(future_data); |
|
|
|
} catch (exception& err) { |
|
|
|
} catch (exception& err) { |
|
|
|
cout << err.what() << endl; |
|
|
|
cout << err.what() << endl; |
|
|
|
} |
|
|
|
} |
|
|
@ -287,7 +341,7 @@ try { |
|
|
|
auto future_status = fs::write("test.txt", "Hello world"); |
|
|
|
auto future_status = fs::write("test.txt", "Hello world"); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
wait(future_status); |
|
|
|
await_(future_status); |
|
|
|
} catch (exception& err) { |
|
|
|
} catch (exception& err) { |
|
|
|
cout << err.what() << endl; |
|
|
|
cout << err.what() << endl; |
|
|
|
} |
|
|
|
} |
|
|
|