AsyncFw 1.2
Async Framework is c++ runtime with timers, poll notifiers, sockets, coroutines, etc.
 
Loading...
Searching...
No Matches
Coroutine/main.cpp

Coroutine example

/*
Copyright (c) 2026 Alexandr Kuzmuk
This file is part of the AsyncFw project. Licensed under the MIT License.
See {Link: LICENSE file https://mit-license.org} in the project root for full license information.
*/
#include <AsyncFw/MainThread>
#include <AsyncFw/ThreadPool>
#include <AsyncFw/Coroutine>
#include <AsyncFw/Log>
AsyncFw::CoroutineAwait await([](AsyncFw::CoroutineHandle h) {
AsyncFw::ThreadPool::async(
[h]() {
logInfo() << "task: run in thread" << ct->name() << ct->id();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
},
[h]() {
logInfo() << "task: resume in thread" << ct->name() << ct->id();
h.resume();
});
});
co_await await;
logNotice() << "task: resumed in thread" << ct->name() << ct->id();
}
int main(int argc, char *argv[]) {
AsyncFw::Instance<AsyncFw::ThreadPool>::create("CoroutineExamplePool");
task();
auto coro_task([]() -> AsyncFw::CoroutineTask {
AsyncFw::CoroutineAwait await([](AsyncFw::CoroutineHandle h) {
AsyncFw::ThreadPool::async(
[h]() {
logInfo() << "coro_task: run in thread" << ct->name() << ct->id();
std::this_thread::sleep_for(std::chrono::milliseconds(15));
},
[h]() {
logInfo() << "coro_task: resume in thread" << ct->name() << ct->id();
h.resume();
});
});
co_await await;
logNotice() << "coro_task: resumed in thread" << ct->name() << ct->id();
AsyncFw::MainThread::exit(0);
});
coro_task();
logNotice() << "Start Applicaiton";
int ret = AsyncFw::MainThread::exec();
logNotice() << "End Applicaiton";
return ret;
}
The AbstractThread class provides the base functionality for thread management.
Definition AbstractThread.h:45
std::string name() const
Returns name of managed thread.
Definition AbstractThread.cpp:679
std::thread::id id() const
Returns unique identifier of managed thread.
Definition AbstractThread.cpp:677
static AbstractThread * currentThread()
Returns a pointer to the AsyncFw::AbstractThread that manages the currently executing thread.
Definition AbstractThread.cpp:297
The CoroutineAwait struct.
Definition Coroutine.h:53
The CoroutineTask struct.
Definition Coroutine.h:20