Skip to content

Commit

Permalink
Barrier unit tests - assert that all threads have hit the barrier
Browse files Browse the repository at this point in the history
ensure correct result and thread safety by using atomic to count threads
  • Loading branch information
cezbloch committed Nov 11, 2024
1 parent f536b82 commit 9827ac3
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions ShaderatorTests/barrier_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
#include "gtest/gtest.h"
#include <Barrier.h>
#include <vector>
#include <atomic>

TEST(BarrierTests, 32_treads_are_properly_blocked)
{
int barrier_size = 32;
Barrier barrier(barrier_size);
int beforeBarrier = 0;
int afterBarrier = 0;

std::atomic<int> beforeBarrier(0);
std::atomic<int> afterBarrier(0);

auto use_barrier = [&]() {
beforeBarrier++;
barrier.Wait();
afterBarrier++;
afterBarrier++;// Atomic fetch add?
};

std::vector<std::thread> threads;
Expand All @@ -27,4 +27,7 @@ TEST(BarrierTests, 32_treads_are_properly_blocked)
for (auto&& thread : threads) {
thread.join();
}

EXPECT_EQ(beforeBarrier.load(), barrier_size);
EXPECT_EQ(afterBarrier.load(), barrier_size);
}

0 comments on commit 9827ac3

Please sign in to comment.