summaryrefslogtreecommitdiffstats
path: root/include/jau
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2021-11-16 14:16:14 +0100
committerSven Gothel <[email protected]>2021-11-16 14:16:14 +0100
commita45d43217b35b099a56f8ac95fc6e467f21fdaf4 (patch)
tree885d79d391c3186490acda12ea75c36f1a9a3fa4 /include/jau
parent7da584910b3f38201571d5817537b69558ec60e1 (diff)
latch: Fix and add unit test
Diffstat (limited to 'include/jau')
-rw-r--r--include/jau/latch.hpp16
1 files changed, 10 insertions, 6 deletions
diff --git a/include/jau/latch.hpp b/include/jau/latch.hpp
index 19cce76..16b3e9a 100644
--- a/include/jau/latch.hpp
+++ b/include/jau/latch.hpp
@@ -40,20 +40,22 @@ namespace jau {
*/
class latch {
private:
- std::mutex mtx_cd;
- std::mutex mtx_cv;
- std::condition_variable cv;
+ mutable std::mutex mtx_cd;
+ mutable std::mutex mtx_cv;
+ mutable std::condition_variable cv;
jau::sc_atomic_size_t count;
public:
/** Returns the maximum value of the internal counter supported by the implementation. */
static constexpr size_t max() noexcept { return std::numeric_limits<size_t>::max(); }
- constexpr latch(const size_t count_) noexcept
+ latch(const size_t count_) noexcept
: count(count_) {}
latch(const latch& o) = delete;
+ size_t value() const noexcept { return count; }
+
/**
* Atomically decrements the internal counter by n
* and notifies all blocked wait() threads if zero is reached.
@@ -69,7 +71,7 @@ namespace jau {
{
std::unique_lock<std::mutex> lock(mtx_cd); // Avoid data-race on concurrent count_down() calls
if( n < count ) {
- count -= n;
+ count = count - n;
notify = false;
} else {
count = 0;
@@ -94,7 +96,9 @@ namespace jau {
void wait() const noexcept {
if( 0 < count ) {
std::unique_lock<std::mutex> lock(mtx_cv);
- cv.wait(lock, [&count]{ return 0 == count; });
+ while( 0 < count ) {
+ cv.wait(lock);
+ }
}
}