diff options
author | Sven Gothel <[email protected]> | 2022-05-15 02:42:12 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2022-05-15 02:42:12 +0200 |
commit | 5c9bdab204224225da317b2025f61af6c15e1bab (patch) | |
tree | f3c500c5b7200dcc84c2ab6c66408063cbfe59f7 /include/jau | |
parent | 231e8dc311a28eccb7f04a2f355e59bcc2a9ac3e (diff) |
jau::latch: Extend functionality with count_up(), allowing to dynamically add *events* to required to complete
Diffstat (limited to 'include/jau')
-rw-r--r-- | include/jau/latch.hpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/include/jau/latch.hpp b/include/jau/latch.hpp index 6b01a1c..9cbfca8 100644 --- a/include/jau/latch.hpp +++ b/include/jau/latch.hpp @@ -43,6 +43,8 @@ namespace jau { /** * Inspired by std::latch of C++20 * + * Adds count_up() as an extension, allowing to dynamically add *events* to required to complete. + * * @see https://en.cppreference.com/w/cpp/thread/latch */ class latch { @@ -111,6 +113,26 @@ namespace jau { } /** + * Atomically increments the internal counter by n. + * + * If internal counter + n is greater than the maximum value of the internal counter, the counter is set to its maximum. + * + * This operation strongly happens-before all calls that are unblocked on this latch. + * + * Extension of std::latch. + * + * @param n the value by which the internal counter is increased, defaults to 1 + */ + void count_up(const size_t n = 1) noexcept { + std::unique_lock<std::mutex> lock(mtx_cd); // Avoid data-race on concurrent count_down() and wait*() calls + if( n <= std::numeric_limits<std::size_t>::max() - count ) { + count = count + n; + } else { + count = std::numeric_limits<std::size_t>::max(); + } + } + + /** * Returns true only if the internal counter has reached zero. * * Compatible with std::latch. |