简体   繁体   English

可以同时获取读取器锁 (SRW) 的线程数是否有限制?

[英]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.在我的应用程序中,为了并发,我在 Windows 上使用 Slim Reader Writer 锁,在 Mac/Linux 上使用 pthread_rwlock_t。

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 .请为 SRW 锁和 pthread_rwlock_t 回答这个问题。 Thanks !谢谢 !

Update :更新

The test creates 16 threads initialized to call the same proc, let say foo().该测试创建了 16 个初始化为调用相同 proc 的线程,比如说 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.问题原来是变量 g_TotalNumberOfThreads(在上面的问题代码中)被编译器优化为不是一直被读取。

Marking the variable g_TotalNumberOfThreads as volatile fixed the problem.将变量 g_TotalNumberOfThreads 标记为 volatile 解决了这个问题。 Thanks !谢谢 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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