summaryrefslogtreecommitdiff
path: root/exercises/087_async4.zig
diff options
context:
space:
mode:
authorChris Boesch <chrboesch@noreply.codeberg.org>2026-04-03 18:11:00 +0200
committerChris Boesch <chrboesch@noreply.codeberg.org>2026-04-03 18:11:00 +0200
commit1c6487c1e79cbe0d59a39b483af8ec44b59c586e (patch)
tree22f774d62d15252e17db75b22be40d7606fe606f /exercises/087_async4.zig
parent25009361533be5e45cf59d9840edf5d13cfb8d6d (diff)
added async-io quiz
Diffstat (limited to 'exercises/087_async4.zig')
-rw-r--r--exercises/087_async4.zig50
1 files changed, 0 insertions, 50 deletions
diff --git a/exercises/087_async4.zig b/exercises/087_async4.zig
deleted file mode 100644
index 50829fc..0000000
--- a/exercises/087_async4.zig
+++ /dev/null
@@ -1,50 +0,0 @@
-//
-// When you have many tasks that don't return individual values,
-// use a Group! A Group is an unordered set of tasks that can
-// only be awaited or canceled as a whole:
-//
-// var group: std.Io.Group = .init;
-// group.async(io, myTask, .{arg1});
-// group.async(io, myTask, .{arg2});
-// try group.await(io); // blocks until ALL tasks finish
-//
-// Important rules:
-// * The return type of functions spawned in a group must be
-// coercible to Cancelable!void (i.e. void, or error{Canceled}!void).
-// * Once you call group.async(), you MUST eventually call
-// group.await() or group.cancel() to release resources.
-// * group.cancel() requests cancellation on ALL members,
-// then waits for them to finish.
-//
-// Unlike Future, Group tasks don't return values to the caller.
-// They're ideal for parallel work that communicates through
-// shared state or side effects (like printing).
-//
-// Fix this program to await all tasks in the group.
-//
-const std = @import("std");
-const print = std.debug.print;
-
-pub fn main(init: std.process.Init) !void {
- const io = init.io;
-
- var group: std.Io.Group = .init;
-
- // Spawn 3 tasks in any order. Each sleeps for (id * 1) seconds
- // before printing, so the output order is deterministic.
- group.async(io, doWork, .{ io, 1 });
- group.async(io, doWork, .{ io, 3 });
- group.async(io, doWork, .{ io, 2 });
-
- // Wait for all tasks to finish.
- // What Group method blocks until all tasks complete?
- try group.???
-
- print("All tasks finished!\n", .{});
-}
-
-fn doWork(io: std.Io, id: u32) void {
- // Sleep ensures deterministic output order.
- io.sleep(std.Io.Duration.fromSeconds(id), .awake) catch return;
- print("Task {} done.\n", .{id});
-}