[DOC] Doc for Thread::ConditionVariable #13024
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Documented the necessity of calling
wait
in a loop. We modified theexample and added a third thread
a2
to make the point clearer.Mentioned spurious wake-up in the doc.
The example in the documentation of
Thread::ConditionVariable
is wrong. Condition variables should use a separate boolean predicate, and should be waited in a loop due to nondeterministic scheduling and spurious wake-ups. This pull request explains these issues and provides the right example. FYI, the documentation of other programming languages or libraries (such as POSIX thread, C++, Java, Rust and Python) also describe such issues and provide relevant examples.The current documentation does not mention whether spurious wake-ups are allowed, but from the function name
rb_thread_sleep_deadly_allow_spurious_wakeup
and the invocation ofsleep_hrtime(..., 0)
, it is apparent that spurious wake-ups are possible. This pull request documented this, too. The presence of spurious wake-ups does not affect the use of condition variables because they always need to be waited in a loop anyway.