From 1617138c721120d4543a0a5b392c979ec9e8956d Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Sun, 16 Apr 2023 21:49:12 +0200 Subject: [PATCH] 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 --- lib/std/Thread/Condition.zig | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/std/Thread/Condition.zig b/lib/std/Thread/Condition.zig index 793779dbdb90d64b59a302ecf03bcf868b86f293..09911df8837bd1b620503b1e6d15dd1944215da1 100644 --- a/lib/std/Thread/Condition.zig +++ b/lib/std/Thread/Condition.zig @@ -18,10 +18,11 @@ //! } //! //! fn producer() void { -//! m.lock(); -//! defer m.unlock(); -//! -//! predicate = true; +//! { +//! m.lock(); +//! defer m.unlock(); +//! predicate = true; +//! } //! c.signal(); //! } //! @@ -37,7 +38,7 @@ //! thread-1: condition.wait(&mutex) //! //! thread-2: // mutex.lock() (without this, the following signal may not see the waiting thread-1) -//! thread-2: // mutex.unlock() (this is optional for correctness once locked above, as signal can be called without holding the mutex) +//! thread-2: // mutex.unlock() (this is optional for correctness once locked above, as signal can be called while holding the mutex) //! thread-2: condition.signal() //! ``` -- 2.54.0