authorgravatar for erik.arvstedt@gmail.comErik Arvstedt <erik.arvstedt@gmail.com> 2023-04-16 21:49:12+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-18 18:38:52-07:00
log1617138c721120d4543a0a5b392c979ec9e8956d
tree55cbdbf0ea5ba484a3fa7cbac8afdbd4b7a6b4fd
parent5bc35fa75bf62bc9a76305fc6bfc3530efba6aec

std.Thread.Condition: optimize example

- Hold the lock for a shorter amount of time - Previously, when holding the lock while signaling, the other, resumed thread could potentially get suspended again immediately because the mutex was still locked. - Fix comment

1 files changed, 6 insertions(+), 5 deletions(-)

lib/std/Thread/Condition.zig+6-5
......@@ -18,10 +18,11 @@
1818//! }
1919//!
2020//! fn producer() void {
21//! m.lock();
22//! defer m.unlock();
23//!
24//! predicate = true;
21//! {
22//! m.lock();
23//! defer m.unlock();
24//! predicate = true;
25//! }
2526//! c.signal();
2627//! }
2728//!
......@@ -37,7 +38,7 @@
3738//! thread-1: condition.wait(&mutex)
3839//!
3940//! thread-2: // mutex.lock() (without this, the following signal may not see the waiting thread-1)
40//! thread-2: // mutex.unlock() (this is optional for correctness once locked above, as signal can be called without holding the mutex)
41//! thread-2: // mutex.unlock() (this is optional for correctness once locked above, as signal can be called while holding the mutex)
4142//! thread-2: condition.signal()
4243//! ```
4344