diff options
| author | Chris Boesch <chrboesch@noreply.codeberg.org> | 2026-04-07 09:18:37 +0200 |
|---|---|---|
| committer | Chris Boesch <chrboesch@noreply.codeberg.org> | 2026-04-07 09:18:37 +0200 |
| commit | 7cb7a9948a9f5c9965fedd59f0cd816f28962fe4 (patch) | |
| tree | 1953e653faba351ab103ec030eacbfc79b7dae6c /exercises/086_async2.zig | |
| parent | 1166f3cfb65d7edc9086c6ba6b8edd4754964b33 (diff) | |
| parent | 3574fd3ae037f34510c6bb6657332b57447ac5b1 (diff) | |
Merge branch 'main' into fix-060
Diffstat (limited to 'exercises/086_async2.zig')
| -rw-r--r-- | exercises/086_async2.zig | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/exercises/086_async2.zig b/exercises/086_async2.zig new file mode 100644 index 0000000..cf376e2 --- /dev/null +++ b/exercises/086_async2.zig @@ -0,0 +1,54 @@ +// +// Now that we know how to get an Io value, let's use it for +// asynchronous execution! +// +// io.async() launches a function and returns a Future. The result +// won't necessarily be available until you call .await() on it: +// +// var future = io.async(someFunction, .{ arg1, arg2 }); +// const result = future.await(io); +// +// The function *may* run immediately or on another thread - +// your code doesn't need to care! That's the beauty of the +// Io abstraction. +// +// IMPORTANT: Every Future MUST be either .await()ed or .cancel()ed. +// Failing to do so leaks resources! A safe pattern is: +// +// var future = io.async(myFn, .{}); +// defer _ = future.cancel(io); // safety net +// // ... later, if we want the result: +// const result = future.await(io); +// // (await after cancel is fine — it just returns the result) +// +// Both .await() and .cancel() block until the task finishes and +// return the result. The difference is that .cancel() also +// requests the task to stop at its next cancellation point. +// Calling either one more than once is safe — subsequent calls +// just return a copy of the result. +// +// Fix this program so that computeAnswer runs asynchronously +// and its result is properly awaited. +// +const std = @import("std"); +const print = std.debug.print; + +pub fn main(init: std.process.Init) !void { + const io = init.io; + + // Launch computeAnswer asynchronously. + var future = io.async(computeAnswer, .{ 6, 7 }); + defer _ = future.cancel(io); // always clean up! + + print("Computing... ", .{}); + + // Now collect the result. What method on Future gives us + // the value, blocking until it's ready? + const answer = future.???(io); + + print("The answer is: {}\n", .{answer}); +} + +fn computeAnswer(a: u32, b: u32) u32 { + return a * b; +} |
