In my application, for the concurrency, I am using Slim Reader Writer lock on Windows and pthread_rwlock_t on Mac/Linux.
I am seeing a weird test failure that makes me wonder if there is a limit on the number of threads that can possess the reader right at a given time ?
Please answer this for both SRW locks and pthread_rwlock_t . Thanks !
The test creates 16 threads initialized to call the same proc, let say foo(). This intermittently hangs.
void foo(int id) //id is the thread ID
/* Acquire shared mutex ... */
AcquireReadLock(g_mutex); // calls AcquireSRWLockShared on windows
AtomicDecrement(&g_TotalNumberOfThreads); // calls InterLockedDecrement()
while (g_TotalNumberOfThreads != 0)
The problem turned out to be that the variable g_TotalNumberOfThreads (in the above code in the question) was being optimized by the compiler for not being read all the time.
Marking the variable g_TotalNumberOfThreads as volatile fixed the problem. Thanks !