diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2022-12-15 01:06:43 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2022-12-28 15:10:40 +0100 |
commit | fddeec60cba806b88c5eb0e4758c3951457518a2 (patch) | |
tree | 8277358947900095470c530f96e8d35a552b1814 /examples/corelib/threads/doc | |
parent | 41c10d4db8614bd27322ff570eb4634c11eb404b (diff) |
Wait conditions example: fix an incorrect condition variable usage
3a449bbb69c9a3c3a5bc6a052f2de98ab79be7e9 amended the code to remove
acquiring a lock when waking up a condition variable. It is fine to
not have a lock associated when waking a condition variable; what I
misunderstood was the scope of the lock, which (and this underlines
the importance of commenting _what exactly_ a lock protects, for
each and ever lock) protected both the buffer as well as the counter
of the buffer. This made my reasoning flawed: it is necessary to keep
the lock while notifying, otherwise the counterpart could verify the
condition isn't satisfied and wait (e.g. see numUsedBytes==0), missing
the wake from the other thread (which could arrive between the check and
the wait).
Amends the previous commit.
Change-Id: If7db2d045331f1b33b976fb6bf6aa9117c41678f
Pick-to: 5.15 6.2 6.4 6.5
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Diffstat (limited to 'examples/corelib/threads/doc')
-rw-r--r-- | examples/corelib/threads/doc/src/waitconditions.qdoc | 7 |
1 files changed, 2 insertions, 5 deletions
diff --git a/examples/corelib/threads/doc/src/waitconditions.qdoc b/examples/corelib/threads/doc/src/waitconditions.qdoc index 7715bd8ab6..ae9e767c13 100644 --- a/examples/corelib/threads/doc/src/waitconditions.qdoc +++ b/examples/corelib/threads/doc/src/waitconditions.qdoc @@ -75,16 +75,13 @@ that the condition \c bufferNotEmpty is true, since \c numUsedBytes is necessarily greater than 0. - The QWaitCondition::wait() function accepts a + We guard all accesses to the \c numUsedBytes variable with a + mutex. In addition, the QWaitCondition::wait() function accepts a mutex as its argument. This mutex is unlocked before the thread is put to sleep and locked when the thread wakes up. Furthermore, the transition from the locked state to the wait state is atomic, to prevent race conditions from occurring. - Accesses to the \c numUsedBytes variable do not need mutex - protection, as that variable is a QAtomicInt; atomic variables - do not participate in data races. - \section1 Consumer Class Let's turn to the \c Consumer class: |