From 1c6487c1e79cbe0d59a39b483af8ec44b59c586e Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Fri, 3 Apr 2026 18:11:00 +0200 Subject: added async-io quiz --- exercises/088_async4.zig | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 exercises/088_async4.zig (limited to 'exercises/088_async4.zig') diff --git a/exercises/088_async4.zig b/exercises/088_async4.zig new file mode 100644 index 0000000..50829fc --- /dev/null +++ b/exercises/088_async4.zig @@ -0,0 +1,50 @@ +// +// 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}); +} -- cgit v1.2.3 From 446da3ce5a1d5ea12bffe5a8b12eaad94e8afeee Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Mon, 6 Apr 2026 12:22:41 +0200 Subject: improvements for async-io --- exercises/088_async4.zig | 4 ++-- patches/patches/088_async4.patch | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'exercises/088_async4.zig') diff --git a/exercises/088_async4.zig b/exercises/088_async4.zig index 50829fc..8298ca1 100644 --- a/exercises/088_async4.zig +++ b/exercises/088_async4.zig @@ -14,7 +14,7 @@ // * 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. +// then blocks until they all finish. // // Unlike Future, Group tasks don't return values to the caller. // They're ideal for parallel work that communicates through @@ -38,7 +38,7 @@ pub fn main(init: std.process.Init) !void { // Wait for all tasks to finish. // What Group method blocks until all tasks complete? - try group.??? + try group.???(io); print("All tasks finished!\n", .{}); } diff --git a/patches/patches/088_async4.patch b/patches/patches/088_async4.patch index 1faf30e..6cf549f 100644 --- a/patches/patches/088_async4.patch +++ b/patches/patches/088_async4.patch @@ -1,10 +1,10 @@ ---- exercises/088_async4.zig 2026-04-01 23:17:31.066443941 +0200 -+++ answers/088_async4.zig 2026-04-01 23:17:39.251612131 +0200 +--- exercises/088_async4.zig 2026-04-06 12:22:06.643385622 +0200 ++++ answers/088_async4.zig 2026-04-06 12:22:11.820491035 +0200 @@ -38,7 +38,7 @@ // Wait for all tasks to finish. // What Group method blocks until all tasks complete? -- try group.??? +- try group.???(io); + try group.await(io); print("All tasks finished!\n", .{}); -- cgit v1.2.3