Skip to content

Commit

Permalink
Merge pull request #10 from DrDub/main
Browse files Browse the repository at this point in the history
Multithreaded test case
  • Loading branch information
DNedic authored Dec 5, 2023
2 parents b2a872d + 558991d commit 8e01b45
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/spsc/ring_buf.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <algorithm>
#include <thread>
#include <catch2/catch_test_macros.hpp>

#include "lockfree.hpp"
Expand Down Expand Up @@ -322,3 +323,36 @@ TEST_CASE("Peek std::span", "[rb_peek_span]") {
REQUIRE(std::equal(std::begin(test_data), std::end(test_data),
std::begin(test_data_read)));
}

TEST_CASE("Multithreaded read/write", "[rb_multi]") {
std::vector<std::thread> threads;
lockfree::spsc::RingBuf<uint64_t, 1024U> rb;
std::vector<uint64_t> written;
std::vector<uint64_t> read;

// consumer
threads.emplace_back([&]() {
bool read_success = false;
uint64_t data[1] = {0};
do {
read_success = rb.Read(data, 1);
if (read_success) {
read.push_back(data[0]);
}
} while (!read_success || data[0] < 2047);
});
// producer
threads.emplace_back([&]() {
uint64_t data[1] = {0};
for (uint64_t idx = 0; idx < 2048; idx++) {
data[0] = idx;
written.push_back(idx);
rb.Write(data, 1);
}
});
for (auto &t : threads) {
t.join();
}
REQUIRE(
std::equal(std::begin(written), std::end(written), std::begin(read)));
}

0 comments on commit 8e01b45

Please sign in to comment.