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

Cleanups for condition_variable's waiting functions #3974

Merged

Conversation

achabense
Copy link
Contributor

@achabense achabense commented Aug 17, 2023

Simplifies , and fixes some minor problems in condition_variable's waiting functions. The correctness can be confirmed by looking through commits in order.

Works towards #718; after this change it will be much easier to fix condition_variable's clock-awareness problem.

condition_variable_any are not effected by this pr.

@achabense achabense requested a review from a team as a code owner August 17, 2023 16:55
stl/inc/mutex Outdated
if (_Abs_time <= _Now) {
return false;
}

Copy link
Contributor Author

@achabense achabense Aug 17, 2023

Choose a reason for hiding this comment

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

About this change:

  1. This is specified by the standard and is in the most maintainable form. The correctness is now obviously only dependent on wait_until(lock,abs_time,pred).
  2. Actually this change fixes a bug - if (_Abs_time <= _Now) should return _Pred() too (this bug is not significant due to timing).
  3. The efficiency loss is not significant IMO, and we will gain some efficiency back after fixing the clock-awareness bug.
  4. condition_variable_any already does like this.
  5. (update) The following benchmark (tested on the unchanged code) shows the main "inefficiency" comes from the inaccuracy of _Cnd_timedwait(including ceiling-cast to milliseconds) / SleepConditionVariableSRW. I'm getting about 1500 us waiting for my call. Compared to this, as wait_until(lock,abs_time) is practically equivalent to a single _Cnd_timedwait call, I think any "efficiency loss" by this change should be negligible.
#include<benchmark/benchmark.h>
#include<mutex>

using namespace std;

void BM_test(benchmark::State& state) {
    condition_variable cv;
    mutex m;
    unique_lock lock{ m };
    for (auto _ : state) {
        cv.wait_for(lock, 500us);
    }
}

BENCHMARK(BM_test);
BENCHMARK_MAIN();

image

Copy link
Member

Choose a reason for hiding this comment

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

(2) is not a bug. return false is returning the result of the most-recent evaluation of _Pred(). (I do agree with the rest of this comment ;))

@StephanTLavavej StephanTLavavej added the enhancement Something can be improved label Aug 17, 2023
@StephanTLavavej StephanTLavavej self-assigned this Aug 17, 2023
@StephanTLavavej StephanTLavavej removed their assignment Sep 15, 2023
stl/inc/mutex Outdated Show resolved Hide resolved
stl/inc/mutex Show resolved Hide resolved
stl/inc/mutex Show resolved Hide resolved
stl/inc/mutex Show resolved Hide resolved
stl/inc/mutex Show resolved Hide resolved
stl/inc/mutex Outdated Show resolved Hide resolved
Copy link
Member

@CaseyCarter CaseyCarter left a comment

Choose a reason for hiding this comment

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

Looks good once I apply the typo fixes.

stl/inc/mutex Outdated Show resolved Hide resolved
stl/inc/mutex Show resolved Hide resolved
stl/inc/mutex Outdated Show resolved Hide resolved
@CaseyCarter CaseyCarter removed their assignment Sep 18, 2023
@StephanTLavavej StephanTLavavej removed their assignment Sep 18, 2023
@StephanTLavavej StephanTLavavej self-assigned this Sep 20, 2023
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit acb342c into microsoft:main Sep 21, 2023
35 checks passed
@StephanTLavavej
Copy link
Member

🧹 ✨ 🎉

@achabense achabense deleted the _Condition_variable_cleanups branch September 22, 2023 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Something can be improved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants