基于 futex 的 4 字节单写入器/多读取器锁
寻找一个基于 futex 的单写入器/多读取器锁的最小实现,不需要超出单个 4 字节 futex 状态变量的空间开销。
一些背景:我有一个应用程序,它将在数千万到数亿个小对象中的每一个中嵌入一个锁。由于锁定的粒度非常细以及应用程序的结构,我预计争用最少。此外,作家将变得稀少,而有竞争力的作家则更加稀少。出于所有这些原因,在这种特定设置中,(理论上)容易出现“听到雷声”现象的解决方案是完全可以接受的。
Looking for a minimal, futex-based implementation of a single-writer/multiple-readers lock requiring no space overhead beyond a single 4-byte futex state variable.
Some background: I have an application which will embed a lock within each of tens to hundreds of millions of small objects. Because of the very fine grained nature of the locking and the structure of the application I anticipate minimal contention. Further, writers will be rare and contending writers rarer still. For all of these reasons, in this particular setting, a solution prone (in theory) to the "thundering heard" phenomenon is quite acceptable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 https://gist.github.com/smokku/653c469d695d60be4fe8170630ba8205 找到我的
实现是只能有一个线程获取写入锁(futex value
0
),锁可以打开(futex value1
),或者可以有多个读取线程( futex 值大于1
)。因此,低于1
的值(只有一个)会阻止 futex 上的读取器和写入器,而高于1
的值仅阻止写入器。解锁线程会唤醒等待线程之一,但您需要小心,不要消耗仅由写入线程唤醒的读取线程。You will find my implementation at https://gist.github.com/smokku/653c469d695d60be4fe8170630ba8205
The idea is that there can be only one thread taking the lock for write (futex value
0
), lock can be open (futex value1
) or there can be many reading threads (futex values greater than1
). So values below1
(there is only one) block both readers and writers on futex, and values above1
block only writers. Unlocking thread wakes one of waiting threads, but you need to be careful not to consume a readers only wake by a writer thread.