Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::Barrier Struct Reference

A synchronization point that blocks threads until the required number of threads have reached it. More...

#include <Threading.h>

Public Member Functions

 Barrier (uint32_t count)
 Creates a barrier that waits for the specified number of threads.
 
void wait ()
 Wait at the barrier until all threads have reached it.
 

Detailed Description

A synchronization point that blocks threads until the required number of threads have reached it.

Example:

constexpr uint32_t numThreads = 8;
constexpr int incrementsPerThread = 1000;
Thread threads[numThreads];
Barrier barrier(numThreads);
struct Context
{
Barrier& barrier;
Atomic<int32_t> sharedCounter;
} ctx = {barrier, 0};
for (uint32_t i = 0; i < numThreads; i++)
{
auto threadFunc = [this, &ctx](Thread& thread)
{
thread.setThreadName(SC_NATIVE_STR("Barrier"));
// Phase 1: Each thread increments the counter
for (int j = 0; j < incrementsPerThread; ++j)
{
ctx.sharedCounter++;
}
ctx.barrier.wait();
// Phase 2: All threads should see the final value
SC_TEST_EXPECT(ctx.sharedCounter == numThreads * incrementsPerThread);
ctx.barrier.wait();
};
SC_TEST_EXPECT(threads[i].start(threadFunc));
}
// Wait for all threads to finish
for (uint32_t i = 0; i < numThreads; i++)
{
SC_TEST_EXPECT(threads[i].join());
}

Constructor & Destructor Documentation

◆ Barrier()

SC::Barrier::Barrier ( uint32_t count)
inline

Creates a barrier that waits for the specified number of threads.

Parameters
countThe number of threads that must reach the barrier before any can continue

Member Function Documentation

◆ wait()

void SC::Barrier::wait ( )

Wait at the barrier until all threads have reached it.


The documentation for this struct was generated from the following file: