| 143 | } |
| 144 | |
| 145 | void SC::ThreadingTest::testRWLock() |
| 146 | { |
| 147 | //! [rwlockSnippet] |
| 148 | constexpr int numReaders = 3; |
| 149 | constexpr int numIterations = 100; |
| 150 | |
| 151 | RWLock rwlock; |
| 152 | int sharedData = 0; |
| 153 | |
| 154 | // Start multiple reader threads |
| 155 | Thread readers[numReaders]; |
| 156 | for (int i = 0; i < numReaders; i++) |
| 157 | { |
| 158 | auto readerFunc = [&](Thread& thread) |
| 159 | { |
| 160 | thread.setThreadName(SC_NATIVE_STR("Reader")); |
| 161 | for (int j = 0; j < numIterations; j++) |
| 162 | { |
| 163 | rwlock.lockRead(); |
| 164 | volatile int value = sharedData; // Prevent optimization |
| 165 | (void)value; |
| 166 | rwlock.unlockRead(); |
| 167 | Thread::Sleep(1); // Small delay to increase contention |
| 168 | } |
| 169 | }; |
| 170 | SC_TEST_EXPECT(readers[i].start(readerFunc)); |
| 171 | } |
| 172 | |
| 173 | // Start a writer thread |
| 174 | Thread writer; |
| 175 | auto writerFunc = [&](Thread& thread) |
| 176 | { |
| 177 | thread.setThreadName(SC_NATIVE_STR("Writer")); |
| 178 | for (int i = 0; i < numIterations; i++) |
| 179 | { |
| 180 | rwlock.lockWrite(); |
| 181 | sharedData++; |
| 182 | rwlock.unlockWrite(); |
| 183 | Thread::Sleep(1); // Small delay to increase contention |
| 184 | } |
| 185 | }; |
| 186 | SC_TEST_EXPECT(writer.start(writerFunc)); |
| 187 | |
| 188 | // Wait for all threads to finish |
| 189 | for (int i = 0; i < numReaders; i++) |
| 190 | { |
| 191 | SC_TEST_EXPECT(readers[i].join()); |
| 192 | } |
| 193 | SC_TEST_EXPECT(writer.join()); |
| 194 | SC_TEST_EXPECT(sharedData == numIterations); |
| 195 | //! [rwlockSnippet] |
| 196 | } |
| 197 | |
| 198 | void SC::ThreadingTest::testBarrier() |
| 199 | { |
nothing calls this directly
no test coverage detected