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
This commit is contained in:
Erik Arvstedt
2023-04-16 21:49:12 +02:00
committed by Andrew Kelley
parent 5bc35fa75b
commit 1617138c72

View File

@@ -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()
//! ```