MCPcopy Create free account
hub / github.com/Pagghiu/SaneCppLibraries / testBarrier

Method testBarrier

Tests/Libraries/Threading/ThreadingTest.cpp:198–239  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

196}
197
198void SC::ThreadingTest::testBarrier()
199{
200 //! [barrierSnippet]
201 constexpr uint32_t numThreads = 8;
202 constexpr int incrementsPerThread = 1000;
203
204 Thread threads[numThreads];
205
206 Barrier barrier(numThreads);
207 struct Context
208 {
209 Barrier& barrier;
210 Atomic<int32_t> sharedCounter;
211 } ctx = {barrier, 0};
212
213 for (uint32_t i = 0; i < numThreads; i++)
214 {
215 auto threadFunc = [this, &ctx](Thread& thread)
216 {
217 thread.setThreadName(SC_NATIVE_STR("Barrier"));
218
219 // Phase 1: Each thread increments the counter
220 for (int j = 0; j < incrementsPerThread; ++j)
221 {
222 ctx.sharedCounter++;
223 }
224 ctx.barrier.wait();
225
226 // Phase 2: All threads should see the final value
227 SC_TEST_EXPECT(ctx.sharedCounter == numThreads * incrementsPerThread);
228 ctx.barrier.wait();
229 };
230 SC_TEST_EXPECT(threads[i].start(threadFunc));
231 }
232
233 // Wait for all threads to finish
234 for (uint32_t i = 0; i < numThreads; i++)
235 {
236 SC_TEST_EXPECT(threads[i].join());
237 }
238 //! [barrierSnippet]
239}
240
241void SC::ThreadingTest::testSemaphore()
242{

Callers

nothing calls this directly

Calls 4

setThreadNameMethod · 0.45
waitMethod · 0.45
startMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected