Skip to content

Commit e928821

Browse files
authored
Merge pull request #13 from DNedic/fix/fix_multithreaded_test
fix(tests): Fix multithreaded RB test
2 parents 8e01b45 + 97a182c commit e928821

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

tests/spsc/ring_buf.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <algorithm>
2-
#include <thread>
32
#include <catch2/catch_test_macros.hpp>
3+
#include <thread>
44

55
#include "lockfree.hpp"
66

@@ -332,23 +332,24 @@ TEST_CASE("Multithreaded read/write", "[rb_multi]") {
332332

333333
// consumer
334334
threads.emplace_back([&]() {
335-
bool read_success = false;
336335
uint64_t data[1] = {0};
337336
do {
338-
read_success = rb.Read(data, 1);
337+
bool read_success = rb.Read(data, 1);
339338
if (read_success) {
340339
read.push_back(data[0]);
341340
}
342-
} while (!read_success || data[0] < 2047);
341+
} while (data[0] < 2047);
343342
});
344343
// producer
345344
threads.emplace_back([&]() {
346-
uint64_t data[1] = {0};
347-
for (uint64_t idx = 0; idx < 2048; idx++) {
348-
data[0] = idx;
349-
written.push_back(idx);
350-
rb.Write(data, 1);
351-
}
345+
uint64_t cnt = 0;
346+
do {
347+
bool write_success = rb.Write(&cnt, 1);
348+
if (write_success) {
349+
written.push_back(cnt);
350+
cnt++;
351+
}
352+
} while (cnt < 2048);
352353
});
353354
for (auto &t : threads) {
354355
t.join();

0 commit comments

Comments
 (0)