62 const size_t count = 10;
63 std::thread tasks[count];
68 REQUIRE(count+1 == completion.
value());
69 REQUIRE(count == my_counter);
71 for(
size_t i=0; i<count; i++) {
72 tasks[i] = std::thread(&TestLatch01::countDown,
this, std::ref(completion), (int64_t)i*1_ms);
76 REQUIRE(0 == completion.
value());
77 REQUIRE(0 == my_counter);
79 for(
auto & task : tasks) {
80 if( task.joinable() ) {
91 const size_t count = 10;
92 std::thread tasks[count];
97 REQUIRE(count+1 == completion.
value());
98 REQUIRE(count == my_counter);
100 for(
size_t i=0; i<count; i++) {
101 tasks[i] = std::thread(&TestLatch01::countDown,
this, std::ref(completion), (int64_t)i*1_ms);
105 REQUIRE(0 == completion.
value());
106 REQUIRE(0 == my_counter);
108 for(
auto & task : tasks) {
109 if( task.joinable() ) {
120 const size_t count = 10;
121 std::thread tasks[count];
124 REQUIRE( 0 == completion.
value() );
127 completion.
set(count);
129 REQUIRE(count == completion.
value());
130 REQUIRE(count == my_counter);
132 for(
size_t i=0; i<count; i++) {
133 tasks[i] = std::thread(&TestLatch01::countDown,
this, std::ref(completion), (int64_t)i*1_ms);
135 REQUIRE(
true == completion.
wait_for(10_s) );
137 REQUIRE(0 == completion.
value());
138 REQUIRE(0 == my_counter);
140 for(
auto & task : tasks) {
141 if( task.joinable() ) {
153 const size_t count = 10;
154 std::thread tasks[count];
159 REQUIRE( 0 == completion.
value() );
160 REQUIRE( 0 == my_counter );
164 for(
size_t i=0; i<count; i++) {
165 tasks[i] = std::thread(&TestLatch01::countUp,
this, std::ref(completion), (int64_t)i*1_ms);
169 while( completion.
value() < count ) {
172 REQUIRE(count == completion.
value());
173 REQUIRE(count == my_counter);
174 for(
auto & task : tasks) {
175 if( task.joinable() ) {
182 for(
size_t i=0; i<count; i++) {
183 tasks[i] = std::thread(&TestLatch01::countDown,
this, std::ref(completion), (int64_t)i*1_ms);
185 REQUIRE(
true == completion.
wait_for(10_s) );
187 REQUIRE(0 == completion.
value());
188 REQUIRE(0 == my_counter);
190 for(
auto & task : tasks) {
191 if( task.joinable() ) {
void test04_up_wait_for()
Testing jau::latch with zero initial count value, count_up(), count_down() and wait().
void test02_down_wait_for()
Testing jau::latch with set initial count value, count_down() and arrive_and_wait_for().
void test03_down_wait_for()
Testing jau::latch default ctor with zero value, then set initial count value, count_down() and wait_...
void test01_down_wait()
Testing jau::latch with set initial count value, count_down() and arrive_and_wait().
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...
size_t value() const noexcept
Return the current atomic internal counter.
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...
void count_up(const size_t n=1) noexcept
Atomically increments the internal counter by n, i.e.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
bool sleep_for(const fraction_timespec &relative_time, const bool monotonic=true, const bool ignore_irq=true) noexcept
sleep_for causes the current thread to block until a specific amount of time has passed.
METHOD_AS_TEST_CASE(TestLatch01::test01_down_wait, "test01_down_wait")