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 @@...@@ -18,10 +18,11 @@
18//! }18//! }
19//!19//!
20//! fn producer() void {20//! fn producer() void {
21//! m.lock();21//! {
22//! defer m.unlock();22//! m.lock();
23//!23//! defer m.unlock();
24//! predicate = true;24//! predicate = true;
25//! }
25//! c.signal();26//! c.signal();
26//! }27//! }
27//!28//!
...@@ -37,7 +38,7 @@...@@ -37,7 +38,7 @@
37//! thread-1: condition.wait(&mutex)38//! thread-1: condition.wait(&mutex)
38//!39//!
39//! thread-2: // mutex.lock() (without this, the following signal may not see the waiting thread-1)40//! 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)
41//! thread-2: condition.signal()42//! thread-2: condition.signal()
42//! ```43//! ```
4344