mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-21 21:53:11 -05:00
add ex087 async 4
This commit is contained in:
parent
3e5647d88c
commit
be279c78f5
3 changed files with 44 additions and 0 deletions
|
@ -426,6 +426,10 @@ const exercises = [_]Exercise{
|
||||||
.main_file = "086_async3.zig",
|
.main_file = "086_async3.zig",
|
||||||
.output = "5 4 3 2 1",
|
.output = "5 4 3 2 1",
|
||||||
},
|
},
|
||||||
|
.{
|
||||||
|
.main_file = "087_async4.zig",
|
||||||
|
.output = "1 2 3 4 5",
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Check the zig version to make sure it can compile the examples properly.
|
/// Check the zig version to make sure it can compile the examples properly.
|
||||||
|
|
30
exercises/087_async4.zig
Normal file
30
exercises/087_async4.zig
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
//
|
||||||
|
// It has probably not escaped your attention that we are no
|
||||||
|
// longer capturing a return value from foo() because the 'async'
|
||||||
|
// keyword returns the frame instead.
|
||||||
|
//
|
||||||
|
// One way to solve this is to use a global variable.
|
||||||
|
//
|
||||||
|
// See if you can make this program print "1 2 3 4 5".
|
||||||
|
//
|
||||||
|
const print = @import("std").debug.print;
|
||||||
|
|
||||||
|
var global_counter: i32 = 0;
|
||||||
|
|
||||||
|
pub fn main() void {
|
||||||
|
var foo_frame = async foo();
|
||||||
|
|
||||||
|
while (global_counter <= 5) {
|
||||||
|
print("{} ", .{global_counter});
|
||||||
|
???
|
||||||
|
}
|
||||||
|
|
||||||
|
print("\n", .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo() void {
|
||||||
|
while (true) {
|
||||||
|
???
|
||||||
|
???
|
||||||
|
}
|
||||||
|
}
|
10
patches/patches/087_async4.patch
Normal file
10
patches/patches/087_async4.patch
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
19c19
|
||||||
|
< ???
|
||||||
|
---
|
||||||
|
> resume foo_frame;
|
||||||
|
27,28c27,28
|
||||||
|
< ???
|
||||||
|
< ???
|
||||||
|
---
|
||||||
|
> global_counter += 1;
|
||||||
|
> suspend;
|
Loading…
Reference in a new issue