Skip to content

[DOC] Doc for Thread::ConditionVariable #13024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025

Conversation

wks
Copy link
Contributor

@wks wks commented Apr 1, 2025

Documented the necessity of calling wait in a loop. We modified the
example 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 of sleep_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.

@wks wks force-pushed the fix/ruby-condvar-doc branch from 564c2e6 to c41edde Compare April 2, 2025 05:43
Documented the necessity of calling `wait` in a loop.  We modified the
example to demonstrate the idiomatic use, and added a third thread `a2`
to demonstrate another reason that necessitates the loop.

Mentioned spurious wake-up in the doc.
@wks wks force-pushed the fix/ruby-condvar-doc branch from c41edde to a662229 Compare April 2, 2025 06:21
@wks
Copy link
Contributor Author

wks commented Apr 2, 2025

@peterzhu2118 I have re-written part of the example and added a third thread a2. I also moved the "Notes:" before the code example to keep the code terse.

@peterzhu2118 peterzhu2118 merged commit ce1dfe8 into ruby:master Apr 2, 2025
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants