mirror of
https://codeberg.org/ziglings/exercises.git
synced 2026-06-08 07:50:00 +00:00
improvements for async-io
This commit is contained in:
@@ -5,20 +5,17 @@
|
|||||||
// them all. The Io backend may run them concurrently:
|
// them all. The Io backend may run them concurrently:
|
||||||
//
|
//
|
||||||
// var f1 = io.async(taskA, .{});
|
// var f1 = io.async(taskA, .{});
|
||||||
|
// defer _ = f1.cancel(io);
|
||||||
// var f2 = io.async(taskB, .{});
|
// var f2 = io.async(taskB, .{});
|
||||||
//
|
// defer _ = f2.cancel(io);
|
||||||
// // Both tasks may be running now!
|
|
||||||
// const a = f1.await(io);
|
// const a = f1.await(io);
|
||||||
// const b = f2.await(io);
|
// const b = f2.await(io);
|
||||||
//
|
//
|
||||||
// There's also io.concurrent() which provides a STRONGER guarantee:
|
// Notice the defer pattern: each async call is immediately
|
||||||
// it ensures the function gets its own unit of concurrency (e.g. a
|
// followed by a defer cancel. This ensures cleanup even if
|
||||||
// real OS thread). But it can fail with error.ConcurrencyUnavailable
|
// we return early or hit an error before reaching await.
|
||||||
// if resources are exhausted.
|
// Since await/cancel are idempotent, the defer is harmless
|
||||||
//
|
// if we've already awaited.
|
||||||
// io.async() is more portable: if no thread is available, it simply
|
|
||||||
// runs the function synchronously. This makes it the right default
|
|
||||||
// for most code.
|
|
||||||
//
|
//
|
||||||
// Fix this program to launch both tasks and collect their results.
|
// Fix this program to launch both tasks and collect their results.
|
||||||
//
|
//
|
||||||
@@ -29,12 +26,14 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
const io = init.io;
|
const io = init.io;
|
||||||
|
|
||||||
// Launch both tasks asynchronously.
|
// Launch both tasks asynchronously.
|
||||||
var future_a = io.async(slowAdd, .{ 10, 20 });
|
var future_a = io.async(slowAdd, .{ 1, 2 });
|
||||||
|
defer _ = future_a.cancel(io);
|
||||||
var future_b = ???(slowMul, .{ 6, 7 });
|
var future_b = ???(slowMul, .{ 6, 7 });
|
||||||
|
defer _ = future_b.cancel(io);
|
||||||
|
|
||||||
// Await both results.
|
// Await both results.
|
||||||
const sum = future_a.await(io);
|
const sum = future_a.await(io);
|
||||||
const product = future_b.???(io);
|
const product = future_b.await(io);
|
||||||
|
|
||||||
print("{} + {} = {}\n", .{ 1, 2, sum });
|
print("{} + {} = {}\n", .{ 1, 2, sum });
|
||||||
print("{} * {} = {}\n", .{ 6, 7, product });
|
print("{} * {} = {}\n", .{ 6, 7, product });
|
||||||
|
|||||||
@@ -1,18 +1,11 @@
|
|||||||
--- exercises/087_async3.zig 2026-04-01 22:51:05.540094851 +0200
|
--- exercises/087_async3.zig 2026-04-05 16:12:48.317265515 +0200
|
||||||
+++ answers/087_async3.zig 2026-04-01 22:50:44.579669189 +0200
|
+++ answers/087_async3.zig 2026-04-05 16:12:52.269343030 +0200
|
||||||
@@ -29,12 +29,12 @@
|
@@ -28,7 +28,7 @@
|
||||||
const io = init.io;
|
|
||||||
|
|
||||||
// Launch both tasks asynchronously.
|
// Launch both tasks asynchronously.
|
||||||
- var future_a = io.async(slowAdd, .{ 10, 20 });
|
var future_a = io.async(slowAdd, .{ 1, 2 });
|
||||||
|
defer _ = future_a.cancel(io);
|
||||||
- var future_b = ???(slowMul, .{ 6, 7 });
|
- var future_b = ???(slowMul, .{ 6, 7 });
|
||||||
+ var future_a = io.async(slowAdd, .{ 1, 2 });
|
|
||||||
+ var future_b = io.async(slowMul, .{ 6, 7 });
|
+ var future_b = io.async(slowMul, .{ 6, 7 });
|
||||||
|
defer _ = future_b.cancel(io);
|
||||||
|
|
||||||
// Await both results.
|
// Await both results.
|
||||||
const sum = future_a.await(io);
|
|
||||||
- const product = future_b.???(io);
|
|
||||||
+ const product = future_b.await(io);
|
|
||||||
|
|
||||||
print("{} + {} = {}\n", .{ 1, 2, sum });
|
|
||||||
print("{} * {} = {}\n", .{ 6, 7, product });
|
|
||||||
|
|||||||
Reference in New Issue
Block a user