diff options
author | Sven Gothel <[email protected]> | 2021-12-05 22:06:38 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-12-05 22:06:38 +0100 |
commit | d7b5331897b5dedab9269de65c5e390009a837c0 (patch) | |
tree | 9215e03be24d6d7f1a9f9554cf34d893a9a585f3 | |
parent | 4d67b951ef9a119c1610c4faf1b9ab9c79a69167 (diff) |
jau:latch::wait_for(..): Reuse absolute timeout time_point, don't wait for another timeout_duration for sporadic wake-ups w/o condition nor timeout hit
-rw-r--r-- | include/jau/latch.hpp | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/include/jau/latch.hpp b/include/jau/latch.hpp index f07dc6b..93b9a49 100644 --- a/include/jau/latch.hpp +++ b/include/jau/latch.hpp @@ -159,11 +159,14 @@ namespace jau { template<typename Rep, typename Period> bool wait_for(const std::chrono::duration<Rep, Period>& timeout_duration) const noexcept { if( 0 < count ) { + std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now() + timeout_duration; std::unique_lock<std::mutex> lock(mtx_cd); while( 0 < count ) { - std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now(); - std::cv_status s = cv.wait_until(lock, t0 + timeout_duration); - if( std::cv_status::timeout == s && 0 < count ) { + std::cv_status s = cv.wait_until(lock, t0); + if( 0 == count ) { + return true; + } + if( std::cv_status::timeout == s ) { return false; } } |