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

<thread>: std::this_thread::sleep_for overflow with high precision durations #5234

Open
alanbirtles opened this issue Jan 14, 2025 · 0 comments · May be fixed by #5237
Open

<thread>: std::this_thread::sleep_for overflow with high precision durations #5234

alanbirtles opened this issue Jan 14, 2025 · 0 comments · May be fixed by #5237
Labels
bug Something isn't working chrono C++20 chrono

Comments

@alanbirtles
Copy link

Describe the bug

Using integer durations with greater than nanosecond precision causes std::this_thread::sleep_for to not sleep.

See https://godbolt.org/z/s37b31MsM and https://stackoverflow.com/questions/79355000/

Command-line test case

C:\Temp>type repro.cpp
#include <chrono>
#include <print>
#include <thread>

using picoseconds = std::chrono::duration<int64_t, std::ratio<1ll, 1'000'000'000'000ll>>;

int main()
{
    auto t1 = std::chrono::steady_clock::now();
    std::this_thread::sleep_for(picoseconds(1'000'000'000'000ll));
    auto t2 = std::chrono::steady_clock::now();
    std::println("{:%S}", t2 - t1);
}

C:\Temp>cl /EHsc /W4 /WX /std:c++latest .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.36.32522 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

/std:c++latest is provided as a preview of language features from the latest C++
working draft, and we're eager to hear about bugs and suggestions for improvements.
However, note that these features are provided as-is without support, and subject
to changes or removal as the working draft evolves. See
https://go.microsoft.com/fwlink/?linkid=2045807 for details.

repro.cpp
Microsoft (R) Incremental Linker Version 14.36.32522.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:repro.exe
repro.obj

C:\Temp>.\repro.exe
00.000057300

Expected behavior

The program should sleep for approximately a second but instead returns instantly.

STL version

MSVC version 19.41.33923

Additional context

A possible fix for this issue is to change:

constexpr auto _Forever = (chrono::steady_clock::time_point::max)();

in _To_absolute_time to:

constexpr auto _Forever = (std::chrono::time_point<std::chrono::steady_clock, decltype(_Abs_time)::duration>::max)();
@StephanTLavavej StephanTLavavej added bug Something isn't working chrono C++20 chrono labels Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working chrono C++20 chrono
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants