在 MacOSX 上,在 C++ 中,如何在没有自旋锁的情况下通过共享内存进行进程间通信?
我有两个进程:
Producer
and
Consumer
它们有一个通常映射的共享内存区域
Memory
现在,Producer 将内容写入内存。消费者从内存中读取内容。
I would prefer Consumer not to spin wait with Memory is empty.
I would prefer Producer not to spin wait when Memory is full.
我该如何实现这一目标?
I have two processes:
Producer
and
Consumer
they have a commonly mmaped shared region of memory
Memory
Now, Producer writes stuff to Memory. Consumer reads stuff from Memory.
I would prefer Consumer not to spin wait with Memory is empty.
I would prefer Producer not to spin wait when Memory is full.
How do I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用互斥体怎么样?由于互斥体将休眠直到资源可用,因此您不会遇到自旋等待问题。
how about using mutexes? since a mutex will sleep until the resource is available, you won't experience the spin-wait problem.
这让人想起哲学家就餐问题。如果您的平台支持,您可以使用条件变量 共享 跨多个进程。通过此类共享条件变量,您的生产者可以在数据可用时向您的消费者发出信号以读取内存,反之亦然。代码> 为空。请记住检查虚假唤醒。
您需要检查 MacOSX pthread 实现是否支持跨进程共享的条件变量。请参阅我对互斥相关问题的回答 来确定如何。答案也适用于条件变量。
This is reminiscent of the Dining Philosophers Problem. If your platform supports it, you could use condition variables shared across multiple processes. With such shared conditional variables your
Producer
could signal yourConsumer
to readMemory
when data is available, and vice versa whenMemory
is empty. Remember to check for a spurious wakeup.You'd need to check if MacOSX pthread implementation supports condition variables shared across processes. See my answer to your mutex related question to determine how. The answer applies for condition variables as well.