Test, debug and fix coroutine
This commit is contained in:
parent
748fd0a586
commit
8d9a56dc8e
@ -311,11 +311,11 @@ mt.tick("string", string("Hello world"));
|
|||||||
|
|
||||||
## Coroutine
|
## Coroutine
|
||||||
|
|
||||||
If `define.hpp` is included, you can initialize coroutines using `asyncable<T>`; if not, just use `boost::asio::awaitable<T>`.
|
If `define.hpp` is included, you can initialize coroutines with `boost::asio::awaitable<T>`.
|
||||||
|
|
||||||
```c++
|
```c++
|
||||||
|
|
||||||
asyncable<int> c2(int a) {
|
awaitable<int> c2(int a) {
|
||||||
co_return a * 2;
|
co_return a * 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -330,7 +330,7 @@ Or using a lambda expression:
|
|||||||
|
|
||||||
```c++
|
```c++
|
||||||
|
|
||||||
async_([]() -> asyncable<void> {
|
async_([]() -> awaitable<void> {
|
||||||
std::cout << "Hello" << std::endl;
|
std::cout << "Hello" << std::endl;
|
||||||
co_await c2(4);
|
co_await c2(4);
|
||||||
co_return;
|
co_return;
|
||||||
@ -354,7 +354,7 @@ If you need the result immediately, you can use a shorter notation
|
|||||||
auto a = await_ ( c2(3));
|
auto a = await_ ( c2(3));
|
||||||
cout << a << endl;
|
cout << a << endl;
|
||||||
|
|
||||||
await_ ([]() -> asyncable<void> {
|
await_ ([]() -> awaitable<void> {
|
||||||
cout << "Hello" << endl;
|
cout << "Hello" << endl;
|
||||||
co_return;
|
co_return;
|
||||||
}());
|
}());
|
||||||
|
@ -97,10 +97,6 @@ Trigger<T...> trigger() {
|
|||||||
#define async_ marcelb::asynco::async_
|
#define async_ marcelb::asynco::async_
|
||||||
#define await_ marcelb::asynco::await_
|
#define await_ marcelb::asynco::await_
|
||||||
|
|
||||||
#if __cplusplus >= 202002L
|
|
||||||
#define asyncable boost::asio::awaitable
|
|
||||||
#endif
|
|
||||||
|
|
||||||
Asynco& asynco_default_runtime();
|
Asynco& asynco_default_runtime();
|
||||||
|
|
||||||
void asynco_default_run();
|
void asynco_default_run();
|
||||||
|
@ -18,7 +18,6 @@ void Asynco::run(uint8_t threads) {
|
|||||||
|
|
||||||
void Asynco::run_on_this() {
|
void Asynco::run_on_this() {
|
||||||
if (!_work) {
|
if (!_work) {
|
||||||
cout << "POKRENE SE KREIRANJE WORK PTR";
|
|
||||||
_work = make_unique<io_service::work>(io_ctx);
|
_work = make_unique<io_service::work>(io_ctx);
|
||||||
}
|
}
|
||||||
io_ctx.run();
|
io_ctx.run();
|
||||||
|
@ -4,18 +4,19 @@ using namespace marcelb::asynco;
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
awaitable<int> c2(int a) {
|
||||||
|
co_return a * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
Asynco asynco; // or global
|
Asynco asynco; // or global
|
||||||
asynco.run(2);
|
asynco.run(2);
|
||||||
|
|
||||||
asyncable<int> c2(int a) {
|
|
||||||
co_return a * 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
asynco.async(c2(4));
|
asynco.async(c2(4));
|
||||||
|
|
||||||
asynco.async([]() -> asyncable<void> {
|
asynco.async([]() -> awaitable<void> {
|
||||||
std::cout << "Hello" << std::endl;
|
std::cout << "Hello" << std::endl;
|
||||||
co_await c2(4);
|
co_await c2(4);
|
||||||
co_return;
|
co_return;
|
||||||
@ -31,7 +32,7 @@ int main() {
|
|||||||
auto a = asynco.await( c2(3));
|
auto a = asynco.await( c2(3));
|
||||||
cout << a << endl;
|
cout << a << endl;
|
||||||
|
|
||||||
asynco.await([]() -> asyncable<void> {
|
asynco.await([]() -> awaitable<void> {
|
||||||
cout << "Hello" << endl;
|
cout << "Hello" << endl;
|
||||||
co_return;
|
co_return;
|
||||||
}());
|
}());
|
||||||
|
@ -4,16 +4,17 @@ using namespace marcelb::asynco;
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
awaitable<int> c2(int a) {
|
||||||
|
co_return a * 2;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
asynco_default_run();
|
asynco_default_run();
|
||||||
|
|
||||||
asyncable<int> c2(int a) {
|
|
||||||
co_return a * 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
async_(c2(4));
|
async_(c2(4));
|
||||||
|
|
||||||
async_([]() -> asyncable<void> {
|
async_([]() -> awaitable<void> {
|
||||||
std::cout << "Hello" << std::endl;
|
std::cout << "Hello" << std::endl;
|
||||||
co_await c2(4);
|
co_await c2(4);
|
||||||
co_return;
|
co_return;
|
||||||
@ -29,7 +30,7 @@ int main() {
|
|||||||
auto a = await_ ( c2(3));
|
auto a = await_ ( c2(3));
|
||||||
cout << a << endl;
|
cout << a << endl;
|
||||||
|
|
||||||
await_ ([]() -> asyncable<void> {
|
await_ ([]() -> awaitable<void> {
|
||||||
cout << "Hello" << endl;
|
cout << "Hello" << endl;
|
||||||
co_return;
|
co_return;
|
||||||
}());
|
}());
|
||||||
|
@ -13,7 +13,7 @@ int main() {
|
|||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
||||||
// stop periodic
|
// stop periodic
|
||||||
inter1.stop();
|
// inter1.stop();
|
||||||
|
|
||||||
// how many times it has expired
|
// how many times it has expired
|
||||||
int ti = inter1.ticks();
|
int ti = inter1.ticks();
|
||||||
@ -27,7 +27,7 @@ int main() {
|
|||||||
}, 10000);
|
}, 10000);
|
||||||
|
|
||||||
// stop delayed
|
// stop delayed
|
||||||
time1.stop();
|
// time1.stop();
|
||||||
|
|
||||||
// is it expired
|
// is it expired
|
||||||
int tt = time1.expired();
|
int tt = time1.expired();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user