mirror of
https://codeberg.org/ziglings/exercises.git
synced 2026-06-08 07:50:00 +00:00
demonstrate the benefit of the mutex
This commit is contained in:
@@ -52,6 +52,12 @@ fn increment(io: std.Io, state: *SharedState, times: u32) void {
|
|||||||
state.mutex.??? catch return;
|
state.mutex.??? catch return;
|
||||||
defer state.mutex.unlock(); // <-- what's missing here?
|
defer state.mutex.unlock(); // <-- what's missing here?
|
||||||
|
|
||||||
|
// Sleep to give the other tasks a chance to run in the meantime.
|
||||||
|
// We do this here only to make nondeterminism more visible.
|
||||||
|
io.sleep(std.Io.Duration.fromMilliseconds(1), .awake) catch {};
|
||||||
|
|
||||||
|
// What happens if you neglect to lock the mutex?
|
||||||
|
|
||||||
state.counter += 1;
|
state.counter += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user