读/写同步
我有一个数据结构,其操作可以分为读操作(例如查找)和写操作(例如插入、删除)。 这些操作应该同步,以便:
- 在执行写操作时,读操作不能执行(除非在同一线程上),但是读操作可以与其他读操作同时执行。
- 当读或写操作正在执行时,写操作不能执行(除非在同一线程上)。
这种同步如何实现呢?
该平台是 win-api,因此 api 的同步对象和互锁函数是基本构建块。
I have a data structure whose operations can be categorized as read operations (e.g. lookup) and write operations (e.g. insertion, removal). These operations should be synchronized so that:
- Read operations can't execute while a write operation is executing (unless on the same thread), however read operations can be executed concurrently with respect to other read operations.
- Write operations can't execute while either read or write operations are executing (unless on the same thread).
How can this kind of synchronization be implemented?
The platform is win-api so the api's synchronization objects and interlocked functions are the basic building blocks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Microsoft 建议的读卡器/写卡器锁实现如下(您必须稍微滚动到标题“读卡器/写卡器锁”):
http://msdn.microsoft.com/en-us/library/ms810427.aspx
仅供参考,供有同样问题但有过问题的人参考.NET 的奢华:
http://msdn.microsoft。 com/en-us/library/system.threading.readerwriterlock.aspx
Microsoft's recommended implementation of a Reader/Writer lock is here (you'll have to scroll a bit, to the header "Reader/Writer locks"):
http://msdn.microsoft.com/en-us/library/ms810427.aspx
For reference, for those who have the same question but who have the luxury of .NET:
http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlock.aspx