是否有同时获取reader锁(SRW)的线程数限制?

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

在我的应用程序中,为了并发,我在 Windows 上使用 Slim Reader 写入器锁,在 Mac/Linux 上使用 pthread_rwlock_t。

我看到一个奇怪的测试失败,这让我想知道在给定时间可以拥有 reader 的线程数量是否有限制?

请为 SRW 锁和 pthread_rwlock_t 回答此问题。谢谢!

更新:

测试创建了 16 个线程,这些线程初始化为调用同一个过程,比如说 foo()。这会间歇性地挂起。

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);
}

问题原来是变量g_TotalNumberOfThreads(在问题上面的代码中)被编译器优化,没有一直被读取。

将变量 g_TotalNumberOfThreads 标记为易失性解决了这个问题。谢谢 !