my ziglings
0

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request 'Demonstrate the benefit of a mutex' (#408) from MatthijsBlom/ziglings:matthijsblom-patch-1 into main

Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/408

Chris Boesch (Apr 19, 2026, 2:21 PM +0200) ca8aee84 f35c9419

+10 -4
+6
exercises/091_async7.zig
··· 52 52 state.mutex.??? catch return; 53 53 defer state.mutex.unlock(); // <-- what's missing here? 54 54 55 + // Sleep to give the other tasks a chance to run in the meantime. 56 + // We do this here only to make nondeterminism more visible. 57 + io.sleep(std.Io.Duration.fromMilliseconds(1), .awake) catch {}; 58 + 59 + // What happens if you neglect to lock the mutex? 60 + 55 61 state.counter += 1; 56 62 } 57 63 }
+4 -4
patches/patches/091_async7.patch
··· 1 - --- exercises/091_async7.zig 2026-04-02 10:50:08.142508099 +0200 2 - +++ answers/091_async7.zig 2026-04-02 10:49:59.629341593 +0200 1 + --- exercises/091_async7.zig 2026-04-18 23:30:40.963951835 +0200 2 + +++ answers/091_async7.zig 2026-04-18 23:33:47.313340585 +0200 3 3 @@ -49,8 +49,8 @@ 4 4 for (0..times) |_| { 5 5 // Acquire the lock before modifying shared state. ··· 9 9 + state.mutex.lock(io) catch return; 10 10 + defer state.mutex.unlock(io); 11 11 12 - state.counter += 1; 13 - } 12 + // Sleep to give the other tasks a chance to run in the meantime. 13 + // We do this here only to make nondeterminism more visible.