diff options
Diffstat (limited to 'exercises')
| -rw-r--r-- | exercises/093_async9.zig | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/exercises/093_async9.zig b/exercises/093_async9.zig index 9d95092..f3c26c9 100644 --- a/exercises/093_async9.zig +++ b/exercises/093_async9.zig @@ -33,7 +33,7 @@ // Let's try a slightly simplified example from signal processing: // Suppose we're looking for the beginning of a signal above the noise // level. To do this, we compare each entry from beginning to end with -// the threshold.To speed things up a bit, we split the signal into +// the threshold. To speed things up a bit, we split the signal into // two halves and have two parallel workers search for them. // Who finds the beginning first "wins" and thus ends the other one. // @@ -45,8 +45,9 @@ const Io = std.Io; const print = std.debug.print; const SearchResult = struct { - worker_id: u8, - index: usize, + found: bool, + worker_id: u8 = 0, + index: usize = 0, }; 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); // 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 }); defer _ = f1.cancel(io); @@ -70,7 +72,8 @@ pub fn main(init: std.process.Init) !void { // Wait for the first result. const result = try queue.getOne(io); - print("Worker {} found signal start over threshold at index {}!\n", .{ result.worker_id, result.index }); + if (result.found) + print("Worker {} found signal start over threshold at index {}!\n", .{ result.worker_id, result.index }); } fn searchThreshold( @@ -87,17 +90,20 @@ fn searchThreshold( // all workers would continue until the end. 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. // print("id: {} - val: {}\n", .{ worker_id, val }); if (val >= threshold) { queue.putOne(io, .{ + .found = true, .worker_id = worker_id, .index = base_offset + i, }) catch return; return; } } -} + // Nothing found + queue.putOneUncancelable(io, .{ .found = false }) catch return; +} |
