29#include <jau/test/catch2_ext.hpp>
59 INFO_STR(
"\n\ntest01\n");
60 const size_t count = 10;
61 std::thread tasks[count];
66 REQUIRE_EQUAL(count+1, completion.
value());
67 REQUIRE_EQUAL(count, my_counter.load());
69 for(
size_t i=0; i<count; i++) {
70 tasks[i] = std::thread(&TestLatch01::countDown,
this, std::ref(completion), (int64_t)i*1_ms);
74 REQUIRE(0 == completion.
value());
75 REQUIRE(0 == my_counter);
77 for(
auto & task : tasks) {
78 if( task.joinable() ) {
88 INFO_STR(
"\n\ntest02\n");
89 const size_t count = 10;
90 std::thread tasks[count];
95 REQUIRE_EQUAL(count+1, completion.
value());
96 REQUIRE_EQUAL(count, my_counter.load());
98 for(
size_t i=0; i<count; i++) {
99 tasks[i] = std::thread(&TestLatch01::countDown,
this, std::ref(completion), (int64_t)i*1_ms);
103 REQUIRE(0 == completion.
value());
104 REQUIRE(0 == my_counter);
106 for(
auto & task : tasks) {
107 if( task.joinable() ) {
117 INFO_STR(
"\n\ntest03\n");
118 const size_t count = 10;
119 std::thread tasks[count];
122 REQUIRE( 0 == completion.
value() );
125 completion.
set(count);
127 REQUIRE_EQUAL(count, completion.
value());
128 REQUIRE_EQUAL(count, my_counter.load());
130 for(
size_t i=0; i<count; i++) {
131 tasks[i] = std::thread(&TestLatch01::countDown,
this, std::ref(completion), (int64_t)i*1_ms);
133 REQUIRE(
true == completion.
wait_for(10_s) );
135 REQUIRE(0 == completion.
value());
136 REQUIRE(0 == my_counter);
138 for(
auto & task : tasks) {
139 if( task.joinable() ) {
150 INFO_STR(
"\n\ntest04\n");
151 const size_t count = 10;
152 std::thread tasks[count];
157 REQUIRE( 0 == completion.
value() );
158 REQUIRE( 0 == my_counter );
162 for(
size_t i=0; i<count; i++) {
163 tasks[i] = std::thread(&TestLatch01::countUp,
this, std::ref(completion), (int64_t)i*1_ms);
167 while( completion.
value() < count ) {
170 REQUIRE_EQUAL(count, completion.
value());
171 REQUIRE_EQUAL(count, my_counter.load());
172 for(
auto & task : tasks) {
173 if( task.joinable() ) {
180 for(
size_t i=0; i<count; i++) {
181 tasks[i] = std::thread(&TestLatch01::countDown,
this, std::ref(completion), (int64_t)i*1_ms);
183 REQUIRE(
true == completion.
wait_for(10_s) );
185 REQUIRE(0 == completion.
value());
186 REQUIRE(0 == my_counter);
188 for(
auto & task : tasks) {
189 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...
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...
ordered_atomic< int, std::memory_order_relaxed > relaxed_atomic_int
Relaxed non-SC atomic integral scalar integer.
fraction< int64_t > fraction_i64
fraction using int64_t as integral type
__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")