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/090_async6.zig | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 exercises/090_async6.zig (limited to 'exercises/090_async6.zig') diff --git a/exercises/090_async6.zig b/exercises/090_async6.zig new file mode 100644 index 0000000..eab03c9 --- /dev/null +++ b/exercises/090_async6.zig @@ -0,0 +1,71 @@ +// +// Sometimes you want to race multiple tasks and act on whichever +// finishes first. That's what Select is for! +// +// Select is like a Group, but lets you receive individual results +// as tasks complete — one at a time: +// +// const Race = std.Io.Select(union(enum) { +// fast: u32, +// slow: u32, +// }); +// +// var buffer: [2]Race.Union = undefined; +// var sel = Race.init(io, &buffer); +// +// sel.async(.fast, fastFn, .{io}); +// sel.async(.slow, slowFn, .{io}); +// +// const winner = try sel.await(); // returns first completed +// switch (winner) { +// .fast => |val| ..., +// .slow => |val| ..., +// } +// sel.cancelDiscard(); // cancel remaining, discard results +// +// The buffer must be large enough for all tasks that might +// complete before you call cancelDiscard(). +// +// Fix this program to receive the winner of the race. +// +const std = @import("std"); +const print = std.debug.print; + +const RaceResult = std.Io.Select(union(enum) { + hare: []const u8, + tortoise: []const u8, +}); + +pub fn main(init: std.process.Init) !void { + const io = init.io; + + var buffer: [2]RaceResult.Union = undefined; + var sel = RaceResult.init(io, &buffer); + + sel.async(.hare, runHare, .{io}); + sel.async(.tortoise, runTortoise, .{io}); + + // Wait for the first finisher. + // What Select method returns the first completed result? + const winner = ???; + + switch (winner) { + .hare => |msg| print("Hare: {s}\n", .{msg}), + .tortoise => |msg| print("Tortoise: {s}\n", .{msg}), + } + + // Clean up the loser — we don't need their result. + sel.cancelDiscard(); +} + +fn runHare(io: std.Io) []const u8 { + // The hare is fast — only 1 second! + io.sleep(std.Io.Duration.fromSeconds(1), .awake) catch return "I got canceled!"; + return "I'm fast!"; +} + +fn runTortoise(io: std.Io) []const u8 { + // The tortoise is slow — 10 seconds. + io.sleep(std.Io.Duration.fromSeconds(10), .awake) catch return "I got canceled!"; + return "Slow and steady..."; +} -- cgit v1.2.3 From aeeb18931da3c8444fc85db4da43e72a0ad2a46a Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Mon, 6 Apr 2026 18:50:57 +0200 Subject: improvements for async-io --- exercises/090_async6.zig | 7 ++++++- patches/patches/090_async6.patch | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'exercises/090_async6.zig') diff --git a/exercises/090_async6.zig b/exercises/090_async6.zig index eab03c9..16fb75f 100644 --- a/exercises/090_async6.zig +++ b/exercises/090_async6.zig @@ -23,6 +23,11 @@ // } // sel.cancelDiscard(); // cancel remaining, discard results // +// As with all async primitives: tasks spawned in a Select MUST +// be cleaned up. Use sel.cancel() to get remaining results one +// by one (for resource cleanup), or sel.cancelDiscard() if you +// don't need them. +// // The buffer must be large enough for all tasks that might // complete before you call cancelDiscard(). // @@ -47,7 +52,7 @@ pub fn main(init: std.process.Init) !void { // Wait for the first finisher. // What Select method returns the first completed result? - const winner = ???; + const winner = try sel.???(); switch (winner) { .hare => |msg| print("Hare: {s}\n", .{msg}), diff --git a/patches/patches/090_async6.patch b/patches/patches/090_async6.patch index 5ac777b..6289708 100644 --- a/patches/patches/090_async6.patch +++ b/patches/patches/090_async6.patch @@ -1,10 +1,10 @@ ---- exercises/090_async6.zig 2026-04-02 10:25:34.016616118 +0200 -+++ answers/090_async6.zig 2026-04-02 10:27:48.827144051 +0200 -@@ -47,7 +47,7 @@ +--- exercises/090_async6.zig 2026-04-06 18:49:37.232023422 +0200 ++++ answers/090_async6.zig 2026-04-06 18:49:22.189720687 +0200 +@@ -52,7 +52,7 @@ // Wait for the first finisher. // What Select method returns the first completed result? -- const winner = ???; +- const winner = try sel.???(); + const winner = try sel.await(); switch (winner) { -- cgit v1.2.3