-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
8 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
docs/main/contents/developer_guides/how_to_guides/coro_guide.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Coroutine Guide | ||
|
||
Starting from LeviLamina 1.0.0, coroutines replace the Scheduler. | ||
|
||
## Usage | ||
|
||
1. Include the header files: | ||
```cpp | ||
#include "ll/api/chrono/GameChrono.h" | ||
#include "ll/api/coro/CoroTask.h" | ||
``` | ||
2. Call `ll::coro::keepThis` in a function, passing a lambda function that returns `ll::coro::CoroTask`. | ||
3. Call the `launch` or `syncLaunch` method after `keepThis`, the latter will block server thread, passing an `Executor` | ||
class, such as `ll::thread::ServerThreadExecutor`. | ||
|
||
!!! tip "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. | ||
|
||
```cpp | ||
#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<> { | ||
co_await 20_tick; | ||
static int i = 0; | ||
while (i < 20) { | ||
std::cout << "This is coro in server thread\n"; | ||
++i; | ||
} | ||
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. | ||
|
||
```cpp | ||
#include "ll/api/chrono/GameChrono.h" | ||
#include "ll/api/coro/CoroTask.h" | ||
#include "ll/api/thread/ThreadPoolExecutor.h" | ||
|
||
void createServerThreadCoro() { | ||
using namespace ll::chrono_literals; | ||
ll::coro::keepThis([]() -> ll::coro::CoroTask<> { | ||
co_await 1s; | ||
static int i = 0; | ||
while (i < 20) { | ||
std::cout << "This is coro in server thread\n"; | ||
++i; | ||
} | ||
co_return; | ||
}).launch(ll::thread::ThreadPoolExecutor::getDefault()); | ||
} | ||
``` | ||
|
67 changes: 67 additions & 0 deletions
67
docs/main/contents/developer_guides/how_to_guides/coro_guide.zh.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 协程指南 | ||
|
||
从LeviLamina 1.0.0开始,协程替代了Scheduler。 | ||
|
||
## 用法 | ||
|
||
1. 引用头文件 | ||
```cpp | ||
#include "ll/api/chrono/GameChrono.h" | ||
#include "ll/api/coro/CoroTask.h" | ||
``` | ||
2. 在函数中调用`ll::coro::keepThis`,传入返回值为`ll::coro::CoroTask`的lambda函数 | ||
3. 在`keepThis`后调用`launch`或`syncLaunch`方法,后者会阻塞服务器线程,传入`Executor`类,比如 | ||
`ll::thread::ServerThreadExecutor` | ||
|
||
!!! tip "可用的Executor" | ||
`ll::thread::ServerThreadExecutor`(ll/api/thread/ServerThreadExecutor.h): 服务器线程 | ||
`ll::thread::ThreadPoolExecutor`(ll/api/thread/ThreadPoolExecutor.h): 线程池 | ||
更多Executor请参见`ll/api/thread/` | ||
|
||
## 示例 | ||
|
||
### ServerThreadExecutor | ||
|
||
以下函数使用协程实现了在服务器线程每20 tick输出字符串到控制台1次,共计20次。 | ||
|
||
```cpp | ||
#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<> { | ||
co_await 20_tick; | ||
static int i = 0; | ||
while (i < 20) { | ||
std::cout << "This is coro in server thread\n"; | ||
++i; | ||
} | ||
co_return; | ||
}).launch(ll::thread::ServerThreadExecutor::getDefault()); | ||
} | ||
``` | ||
|
||
### ThreadPoolExecutor | ||
|
||
以下函数使用协程实现了在线程池中每秒输出字符串到控制台1次,共计20次。 | ||
|
||
```cpp | ||
#include "ll/api/chrono/GameChrono.h" | ||
#include "ll/api/coro/CoroTask.h" | ||
#include "ll/api/thread/ThreadPoolExecutor.h" | ||
|
||
void createServerThreadCoro() { | ||
using namespace ll::chrono_literals; | ||
ll::coro::keepThis([]() -> ll::coro::CoroTask<> { | ||
co_await 1s; | ||
static int i = 0; | ||
while (i < 20) { | ||
std::cout << "This is coro in server thread\n"; | ||
++i; | ||
} | ||
co_return; | ||
}).launch(ll::thread::ThreadPoolExecutor::getDefault()); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters