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

Method testAtomicMultiThreaded

Tests/Libraries/Threading/AtomicTest.cpp:172–250  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

170}
171
172void SC::AtomicTest::testAtomicMultiThreaded()
173{
174 // Test fetch_add
175 {
176 Atomic<int32_t> counter(0);
177 Thread threads[4];
178 for (auto& thread : threads)
179 {
180 SC_TEST_EXPECT(thread.start(
181 [&](Thread&)
182 {
183 for (int k = 0; k < 1000; ++k)
184 {
185 counter.fetch_add(1, memory_order_relaxed);
186 }
187 }));
188 }
189 for (auto& thread : threads)
190 {
191 SC_TEST_EXPECT(thread.join());
192 }
193 SC_TEST_EXPECT(counter.load() == 4000);
194 }
195
196 // Test compare_exchange_strong
197 {
198 Atomic<int32_t> value(0);
199 Atomic<int32_t> successes(0);
200 Thread threads[4];
201 for (auto& thread : threads)
202 {
203 SC_TEST_EXPECT(thread.start(
204 [&](Thread&)
205 {
206 int32_t expected = 0;
207 if (value.compare_exchange_strong(expected, 1))
208 {
209 successes.fetch_add(1);
210 }
211 }));
212 }
213 for (auto& thread : threads)
214 {
215 SC_TEST_EXPECT(thread.join());
216 }
217 SC_TEST_EXPECT(value.load() == 1);
218 SC_TEST_EXPECT(successes.load() == 1);
219 }
220
221 // Test store(release) / load(acquire) ordering
222 {
223 struct Context
224 {
225 Atomic<bool> flag = false;
226 int data = 0;
227 } ctx;
228 Thread producer;
229 SC_TEST_EXPECT(producer.start(

Callers

nothing calls this directly

Calls 6

fetch_addMethod · 0.80
startMethod · 0.45
joinMethod · 0.45
loadMethod · 0.45
storeMethod · 0.45

Tested by

no test coverage detected