读和写之间的互斥
我在关键部分使用互斥体。我有一个设计限制, 1) 如果 2 个读取发生在 2 个不同的线程中,则临界区应该对两个线程都可用。 2)如果发生写入,则应阻止其他线程上的读取 3)如果发生读取,则应阻止其他线程上的写入。
我不知道如何使用 Createmutex、openmutex、waitforsingleobject 和 releasemutex api 来执行此操作。
I am using a mutex for the critical section. I have a design constraint that,
1) If 2 reads occur in 2 different thread, then critical section should be available for both.
2) If there is a write happening, then read on other thread should be blocked
3) If there is a read happening, then write on other thread should be blocked.
I dont know to do this with Createmutex, openmutex, waitforsingleobject and releasemutex apis.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找的是读写器锁。互联网上有一些它的实现,例如这个。
What you are looking for is a Reader-Writer lock. There are some implementations of it on the internet, for example this one.
请查看以下链接,了解此本机 Windows api 是否满足您的要求:
Slim 读/写器 (SRW) 锁
Take a look at the following link to see whether this native windows api meets your requirements:
Slim Reader/Writer (SRW) Locks
如果您的同步/关键部分问题仅限于单个进程和多个线程,我建议您使用 关键部分对象而不是互斥对象。您在 msdn 链接的页面上有几个示例。阅读链接的 msdn 页面,如果您掌握了基本的并发同步概念和所需的 Windows Api 知识,那么即使不阅读,您也应该立即上手。从 API 的角度来看,互斥体和关键部分没有什么特别之处。
If your synchronization/critical section issue is limited to a single process and many threads, i recommend you use Critical Section Objects instead of Mutex Objects. You have a couple of examples on the pages linked from msdn. Read the linked msdn pages and if you have the basic concurrency synchronization concepts and the required Windows Api knowledge under your belt, you should be rolling in no time, if not read around. There's nothing that special about Mutexes and Critical Sections from the api standpoint.