improvements for async-io

This commit is contained in:
Chris Boesch
2026-04-06 19:57:32 +02:00
parent 55a4841b07
commit 882c6aa0ab
2 changed files with 59 additions and 53 deletions

View File

@@ -1,38 +1,42 @@
--- exercises/095_quiz_async.zig 2026-04-03 18:04:53.577391455 +0200
+++ answers/095_quiz_async.zig 2026-04-03 18:05:42.570392172 +0200
--- exercises/095_quiz_async.zig 2026-04-06 19:55:17.111817364 +0200
+++ answers/095_quiz_async.zig 2026-04-06 19:56:16.063974543 +0200
@@ -51,7 +51,7 @@
fn addReading(self: *GardenWeather, io: std.Io, reading: Reading) void {
// Bug 1: The collector needs to lock before modifying
// shared state. What Mutex method acquires the lock?
self.mutex.lock(io) catch return;
- self.mutex.???(io) catch return;
+ defer self.mutex.unlock(io);
+ self.mutex.lock(io) catch return;
defer self.mutex.unlock(io);
switch (reading.sensor_type) {
.thermometer => self.temperature = reading.value,
@@ -79,9 +79,9 @@
// Bug 2: io.async doesn't guarantee a separate thread.
// Which Io method guarantees true concurrency?
// (Don't forget: it can fail, so you need 'try'!)
- try sensors.???(io, sensor, .{ io, &queue, .thermometer, 20 });
- try sensors.???(io, sensor, .{ io, &queue, .hygrometer, 60 });
- try sensors.???(io, sensor, .{ io, &queue, .anemometer, 10 });
+ try sensors.concurrent(io, sensor, .{ io, &queue, .thermometer, 20 });
+ try sensors.concurrent(io, sensor, .{ io, &queue, .hygrometer, 60 });
+ try sensors.concurrent(io, sensor, .{ io, &queue, .anemometer, 10 });
@@ -78,7 +78,7 @@
// Bug 2: The collector needs guaranteed concurrency.
// What method ensures a separate unit of concurrency?
// (Don't forget: it can fail!)
- var collector_future = try io.???(collector, .{ io, &queue, &weather });
+ var collector_future = try io.concurrent(collector, .{ io, &queue, &weather });
defer _ = collector_future.cancel(io);
// Sensor group: the sensors can use async — they just need
@@ -91,7 +91,7 @@
// Collector group: processes readings from the queue.
var collectors: std.Io.Group = .init;
@@ -90,7 +90,6 @@
// Bug 3: Wait for ALL sensors to finish sending their readings.
// What Group method blocks until all tasks complete?
try sensors.await(io);
- // try sensors.???(io);
- try sensors.???(io);
+ try sensors.await(io);
// All sensors done — close the queue so the collector knows
// there's no more data coming.
@@ -104,8 +103,8 @@
@@ -99,15 +99,14 @@
// Wait for the collector to drain the remaining queue.
_ = collector_future.await(io);
- // _ = collector_future.???(io);
// Now write the garden report. This is critical — it must
// NOT be interrupted, even if something tries to cancel us!
//
// Bug 4: Protect this section from cancellation.
// Bug 5: Protect this section from cancellation.
// What Io method swaps the cancel protection state?
- const old_protection = io.???(.blocked);
- defer _ = io.???(old_protection);
@@ -41,9 +45,9 @@
printGardenReport(&weather);
}
@@ -127,7 +126,7 @@
@@ -129,7 +128,7 @@
// Bug 5: Send the reading into the queue.
// Bug 6: Send the reading into the queue.
// What Queue method sends a single element?
- queue.???(io, reading) catch return;
+ queue.putOne(io, reading) catch return;