-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathasync_await_simple.zig
More file actions
38 lines (29 loc) · 843 Bytes
/
Copy pathasync_await_simple.zig
File metadata and controls
38 lines (29 loc) · 843 Bytes
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
const std = @import("std");
const debug = std.debug.print;
const rand = std.rand;
const fbaAlloc = std.heap.FixedBufferAllocator;
pub const io_mode = .evented;
pub fn main() !void {
var i: usize = 0;
var pa = std.heap.page_allocator;
var buf = try pa.alloc(@Frame(print), 100);
defer pa.free(buf);
debug("{d} bytes allocated\n", .{@sizeOf(@Frame(print)) * 100});
const pcg = rand.Pcg.init(4);
var random_number = rand.Random{
.fillFn = pcg.random.fillFn,
};
while (i < 100) : (i += 1) {
var n = random_number.uintLessThan(usize, 1_000_000_000);
buf[i] = async print(i, n);
}
i = 0;
while (i < 100) : (i += 1) {
await buf[i];
}
debug("Finish!\n", .{});
}
fn print(i: usize, n: usize) void {
std.time.sleep(n + 10);
debug("{d}\n", .{i});
}