Coroutine Guide
Starting from LeviLamina 1.0.0, coroutines replace the Scheduler.
Usage
- Include the header files:
C++
#include "ll/api/chrono/GameChrono.h" #include "ll/api/coro/CoroTask.h" - Call
ll::coro::keepThisin a function, passing a lambda function that returnsll::coro::CoroTask. - Call the
launchorsyncLaunchmethod afterkeepThis, the latter will block server thread, passing anExecutorclass, such asll::thread::ServerThreadExecutor.
Available Executors
ll::thread::ServerThreadExecutor (ll/api/thread/ServerThreadExecutor.h): Server thread
ll::thread::ThreadPoolExecutor (ll/api/thread/ThreadPoolExecutor.h): Thread pool
For more Executors, refer to ll/api/thread/.
Examples
ServerThreadExecutor
The following function uses a coroutine to print a string to the console every 20 ticks on the server thread, for a total of 20 times.
C++
#include "ll/api/chrono/GameChrono.h"
#include "ll/api/coro/CoroTask.h"
#include "ll/api/thread/ServerThreadExecutor.h"
void createServerThreadCoro() {
using namespace ll::chrono_literals;
ll::coro::keepThis([]() -> ll::coro::CoroTask<> {
for (int i = 0; i < 20; ++i) {
co_await 20_tick;
std::cout << "This is coro in server thread\n";
}
co_return;
}).launch(ll::thread::ServerThreadExecutor::getDefault());
}ThreadPoolExecutor
The following function uses a coroutine to print a string to the console every second in the thread pool, for a total of 20 times.
C++
#include "ll/api/chrono/GameChrono.h"
#include "ll/api/coro/CoroTask.h"
#include "ll/api/thread/ThreadPoolExecutor.h"
void createThreadPoolCoro() {
using namespace ll::chrono_literals;
ll::coro::keepThis([]() -> ll::coro::CoroTask<> {
for (int i = 0; i < 20; ++i) {
co_await 1s;
std::cout << "This is coro in thread pool\n";
}
co_return;
}).launch(ll::thread::ThreadPoolExecutor::getDefault());
}