Skip to content
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

Fix inaccurate documentation of wait_while and wait_timeout_while #121405

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Jupeyy
Copy link

@Jupeyy Jupeyy commented Feb 21, 2024

fixes #121388

Sometimes code is more clear than documentation, cppreference is doing that:

quote start

Equivalent to

while (!stop_waiting())
{
    wait(lock);
}

(https://en.cppreference.com/w/cpp/thread/condition_variable/wait)

quote end

But the current documentation can be interpreted incorrectly. For example if you wait for a condition that is already false, the current documentation suggests that the current thread will still block.

@rustbot
Copy link
Collaborator

rustbot commented Feb 21, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Mark-Simulacrum (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 21, 2024
/// will be blocked until this condition variable receives a notification.
/// If the provided condition evaluates to `false`, and the thread is not
/// blocked as a result of an evaluation to `true` by the provided condition,
/// then the operation is completed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the old phrasing was clearer. It's technically true that we evaluate the condition before blocking, but the right mental model for a condition variable is that it blocks until the condition is false.

The new wording also suggests to me that we are continuously evaluating the condition while waiting for a notification, which isn't accurate: the condition is only evaluated in response to notifications (or spurious wakeups).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the right mental model for a condition variable is that it blocks until the condition is false.

That would be really bad.

Imagine i have aVec of events. every time i push events i also signal the condition variable.

Now at cleanup I might want to check if all events were handled, else wait.

cond_var.wait_while(&mut g, |g| !g.events.is_empty());

but if in this case events is empty, a blocking would lead to infinite waiting.

The new wording also suggests to me that we are continuously evaluating the condition while waiting for a notification

I'd glady take better suggestions :D
I simply tried to make while condition is true, thread will block until notification sound a bit more english.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy to see the same code block as in the implementation (perhaps a bit simplified if needed) put into the documentation to help further clarify exactly what the function does if the current docs aren't clear enough.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Condvar::wait_while documentation is inaccurate
3 participants