简体   繁体   中英

Is there a limit on the number of threads that can simultaneously acquire reader lock (SRW)?

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 !

Update :

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)
        ;   //spin
    ReleaseReadLock(g_mutex);
}

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM