forked from ratfactor/ziglings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path091_async8.zig
41 lines (33 loc) · 971 Bytes
/
091_async8.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// You have doubtless noticed that 'suspend' requires a block
// expression like so:
//
// suspend {} // todo x: suspend 块语句表达式
//
// The suspend block executes when a function suspends. To get
// sense for when this happens, please make the following
// program print the string
//
// "ABCDEF"
//
const print = @import("std").debug.print;
//
// todo x: 异步 IO(suspend + async + resume), 此例子非常典型, 完全理解每条语句执行顺序
//
pub fn main() void {
print("A", .{}); // [step 1]
//
// TODO X: 这个练习, 非常经典, 理解所有语句的执行顺序
//
var frame = async suspendable(); // todo x: 异步, 线程切换 [step 2]
print("D", .{}); // [step5]
resume frame; // todo x: 恢复
print("F", .{});
}
fn suspendable() void {
print("B", .{}); // [step3]
suspend { // TODO X: 异步, 挂起
print("C", .{}); // [step4]
}
print("E", .{}); // [step6]
}