| 196 | } |
| 197 | |
| 198 | void 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 | |
| 241 | void SC::ThreadingTest::testSemaphore() |
| 242 | { |
nothing calls this directly
no test coverage detected