Skip to content

Commit c1f1958

Browse files
committed
Serialize factory allocation in concurrent send-buffer test
1 parent 7105df4 commit c1f1958

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

‎testing/int/sendbuffer.cpp‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "send_buffer.h"
3030
#include <atomic>
3131
#include <catch2/catch_all.hpp>
32+
#include <mutex>
3233
#include <thread>
3334
#include <vector>
3435

@@ -136,6 +137,7 @@ TEST_CASE("multi-threaded send_buffer stress", "[queue][regression][send_buffer]
136137
const int iterations_per_thread = 200;
137138

138139
lsl::factory fac(lsl_channel_format_t::cft_float32, 4, buffer_size * 2);
140+
std::mutex factory_mut;
139141
auto sendbuf = std::make_shared<lsl::send_buffer>(buffer_size);
140142

141143
std::vector<std::shared_ptr<lsl::consumer_queue>> queues;
@@ -148,7 +150,13 @@ TEST_CASE("multi-threaded send_buffer stress", "[queue][regression][send_buffer]
148150
// Producer threads push samples concurrently
149151
auto producer = [&]() {
150152
for (int i = 0; i < iterations_per_thread; ++i) {
151-
auto sample = fac.new_sample(static_cast<double>(i), true);
153+
lsl::sample_p sample;
154+
{
155+
// The factory permits only one allocator at a time. Keep the
156+
// send_buffer pushes concurrent without racing its sample pool.
157+
std::lock_guard<std::mutex> lock(factory_mut);
158+
sample = fac.new_sample(static_cast<double>(i), true);
159+
}
152160
sendbuf->push_sample(sample);
153161
push_count.fetch_add(1, std::memory_order_relaxed);
154162
}

0 commit comments

Comments
 (0)