30#include <condition_variable>
52 mutable std::mutex mtx_cd;
53 mutable std::condition_variable cv;
77 latch(
const size_t count_) noexcept
94 size_t value() const noexcept {
return count; }
111 std::unique_lock<std::mutex> lock(mtx_cd);
137 std::unique_lock<std::mutex> lock(mtx_cd);
154 void set(
const size_t n)
noexcept {
155 std::unique_lock<std::mutex> lock(mtx_cd);
177 std::unique_lock<std::mutex> lock(mtx_cd);
212 std::unique_lock<std::mutex> lock(mtx_cd);
215 std::cv_status s =
wait_until(cv, lock, timeout_time);
219 if( std::cv_status::timeout == s ) {
260 template<
typename Rep,
typename Period>
261 bool wait_for(
const std::chrono::duration<Rep, Period>& timeout_duration)
const noexcept {
263 std::unique_lock<std::mutex> lock(mtx_cd);
264 std::chrono::steady_clock::time_point timeout_time = std::chrono::steady_clock::now() + timeout_duration;
266 std::cv_status s = cv.wait_until(lock, timeout_time );
270 if( std::cv_status::timeout == s ) {
294 template<
typename Rep,
typename Period>
295 bool arrive_and_wait_for(
const std::chrono::duration<Rep, Period>& timeout_duration,
const size_t n = 1) noexcept {
Inspired by std::latch of C++20.
void arrive_and_wait(const size_t n=1) noexcept
Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until th...
latch(const latch &o)=delete
No copy constructor nor move constructor.
latch(const size_t count_) noexcept
Initialize instance with given counter.
static constexpr size_t max() noexcept
Returns the maximum value of the internal counter supported by the implementation.
size_t value() const noexcept
Return the current atomic internal counter.
bool arrive_and_wait_for(const std::chrono::duration< Rep, Period > &timeout_duration, const size_t n=1) noexcept
Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until th...
bool try_wait() const noexcept
Returns true only if the internal counter has reached zero.
void set(const size_t n) noexcept
Atomically set the internal counter by n.
bool wait_for(const fraction_i64 &timeout_duration) const noexcept
Blocks the calling thread until the internal counter reaches 0 or the given timeout duration has expi...
void count_down(const size_t n=1) noexcept
Atomically decrements the internal counter by n and notifies all blocked wait() threads if zero is re...
bool arrive_and_wait_for(const fraction_i64 &timeout_duration, const size_t n=1) noexcept
Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until th...
latch() noexcept
Initialize instance with counter zero.
void count_up(const size_t n=1) noexcept
Atomically increments the internal counter by n, i.e.
void wait() const noexcept
Blocks the calling thread until the internal counter reaches 0.
bool wait_for(const std::chrono::duration< Rep, Period > &timeout_duration) const noexcept
Blocks the calling thread until the internal counter reaches 0 or the given timeout duration has expi...
fraction_timespec getMonotonicTime() noexcept
Returns current monotonic time since Unix Epoch 00:00:00 UTC on 1970-01-01.
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
std::cv_status wait_until(std::condition_variable &cv, std::unique_lock< std::mutex > &lock, const fraction_timespec &absolute_time, const bool monotonic=true) noexcept
wait_until causes the current thread to block until the condition variable is notified,...
Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platfor...