added cancelation if nothing found

This commit is contained in:
Chris Boesch
2026-04-14 22:07:57 +02:00
parent ab3c498226
commit e412619d88
2 changed files with 16 additions and 15 deletions

View File

@@ -45,8 +45,9 @@ const Io = std.Io;
const print = std.debug.print; const print = std.debug.print;
const SearchResult = struct { const SearchResult = struct {
worker_id: u8, found: bool,
index: usize, worker_id: u8 = 0,
index: usize = 0,
}; };
pub fn main(init: std.process.Init) !void { pub fn main(init: std.process.Init) !void {
@@ -61,6 +62,7 @@ pub fn main(init: std.process.Init) !void {
var queue = Io.Queue(SearchResult).init(&buf); var queue = Io.Queue(SearchResult).init(&buf);
// Launch two workers, each searching half the array. // Launch two workers, each searching half the array.
// Remember, we want them to be guaranteed separate units of concurrency.
var f1 = ???(searchRange, .{ data[0..mid], target, 0, 0, &queue, io }); var f1 = ???(searchRange, .{ data[0..mid], target, 0, 0, &queue, io });
defer _ = f1.cancel(io); defer _ = f1.cancel(io);
@@ -70,6 +72,7 @@ pub fn main(init: std.process.Init) !void {
// Wait for the first result. // Wait for the first result.
const result = try queue.getOne(io); const result = try queue.getOne(io);
if (result.found)
print("Worker {} found signal start over threshold at index {}!\n", .{ result.worker_id, result.index }); print("Worker {} found signal start over threshold at index {}!\n", .{ result.worker_id, result.index });
} }
@@ -87,17 +90,20 @@ fn searchThreshold(
// all workers would continue until the end. // all workers would continue until the end.
io.sleep(Io.Duration.fromMilliseconds(1), .awake) catch return; io.sleep(Io.Duration.fromMilliseconds(1), .awake) catch return;
// To test this, you can view the work of the workers // To test this, you can uncomment this to view the work of the workers
// and then comment out the pause. // and then comment out the pause.
// print("id: {} - val: {}\n", .{ worker_id, val }); // print("id: {} - val: {}\n", .{ worker_id, val });
if (val >= threshold) { if (val >= threshold) {
queue.putOne(io, .{ queue.putOne(io, .{
.found = true,
.worker_id = worker_id, .worker_id = worker_id,
.index = base_offset + i, .index = base_offset + i,
}) catch return; }) catch return;
return; return;
} }
} }
}
// Nothing found
queue.putOneUncancelable(io, .{ .found = false }) catch return;
}

View File

@@ -1,9 +1,9 @@
--- exercises/093_async9.zig 2026-04-14 09:50:05.694073287 +0200 --- exercises/093_async9.zig 2026-04-14 22:06:23.644776893 +0200
+++ answers/093_async9.zig 2026-04-14 09:49:58.604934765 +0200 +++ answers/093_async9.zig 2026-04-14 22:06:30.606912044 +0200
@@ -61,10 +61,10 @@ @@ -63,10 +63,10 @@
var queue = Io.Queue(SearchResult).init(&buf);
// Launch two workers, each searching half the array. // Launch two workers, each searching half the array.
// Remember, we want them to be guaranteed separate units of concurrency.
- var f1 = ???(searchRange, .{ data[0..mid], target, 0, 0, &queue, io }); - var f1 = ???(searchRange, .{ data[0..mid], target, 0, 0, &queue, io });
+ var f1 = try io.concurrent(searchThreshold, .{ io, data[0..mid], threshold, 0, 0, &queue }); + var f1 = try io.concurrent(searchThreshold, .{ io, data[0..mid], threshold, 0, 0, &queue });
defer _ = f1.cancel(io); defer _ = f1.cancel(io);
@@ -13,8 +13,3 @@
defer _ = f2.cancel(io); defer _ = f2.cancel(io);
// Wait for the first result. // Wait for the first result.
@@ -100,4 +100,3 @@
}
}
}
-