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

<condition_variable>: condition_variable_any::wait_until may cause infinite wait #3756

Closed
achabense opened this issue Jun 8, 2023 · 0 comments · Fixed by #3761
Closed

<condition_variable>: condition_variable_any::wait_until may cause infinite wait #3756

achabense opened this issue Jun 8, 2023 · 0 comments · Fixed by #3761
Labels
bug Something isn't working fixed Something works now, yay!

Comments

@achabense
Copy link
Contributor

achabense commented Jun 8, 2023

The following code will cause infinite wait (unexpectedly).

#include<thread>
#include<condition_variable>
#include<mutex>

int main() {
	using namespace std;
	using ull_sec = std::chrono::duration<unsigned long long>;
	auto ull_point_sec = chrono::time_point_cast<ull_sec>(chrono::system_clock::now());
	std::this_thread::sleep_for(2s);

	std::mutex m;
	std::unique_lock lock{m};
	std::condition_variable_any cond;
	cond.wait_until(lock, ull_point_sec);//infinite wait.
}

In this case, in wait_until, _Abs_time - _Clock::now() will result in a large unsigned value. A quick fix would be adding a comparison before calling wait_for (return at once if _Abs_time <= now).

In the current implementation, there are both wait_until calling wait_for(see below) and wait_for calling wait_until(example). For better consistency and conformance to the standard(ref), a thorough fix would be ensuring that every wait_for will call a matching wait_until.

template <class _Lock, class _Clock, class _Duration>
cv_status wait_until(_Lock& _Lck, const chrono::time_point<_Clock, _Duration>& _Abs_time) {
// wait until time point
#if _HAS_CXX20
static_assert(chrono::is_clock_v<_Clock>, "Clock type required");
#endif // _HAS_CXX20
return wait_for(_Lck, _Abs_time - _Clock::now());
}

@achabense achabense changed the title <condition_variable>: condition_variable_any::wait_for should be defined based on wait_until condition_variable_any::wait_for should be defined using wait_until Jun 9, 2023
@achabense achabense changed the title condition_variable_any::wait_for should be defined using wait_until <condition_variable>: condition_variable_any::wait_until may result in infinite wait unexpectedly Jun 9, 2023
@achabense achabense changed the title <condition_variable>: condition_variable_any::wait_until may result in infinite wait unexpectedly <condition_variable>: condition_variable_any::wait_until may cause infinite wait Jun 9, 2023
@StephanTLavavej StephanTLavavej added the bug Something isn't working label Jun 11, 2023
@StephanTLavavej StephanTLavavej added the fixed Something works now, yay! label Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Something works now, yay!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants