在 MacOSX 上,在 C++ 中,如何在没有自旋锁的情况下通过共享内存进行进程间通信?

发布于 2024-08-23 14:39:55 字数 334 浏览 2 评论 0原文

我有两个进程:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓬勃野心 2024-08-30 14:39:55

使用互斥体怎么样?由于互斥体将休眠直到资源可用,因此您不会遇到自旋等待问题。

how about using mutexes? since a mutex will sleep until the resource is available, you won't experience the spin-wait problem.

埋情葬爱 2024-08-30 14:39:55

这让人想起哲学家就餐问题。如果您的平台支持,您可以使用条件变量 共享 跨多个进程。通过此类共享条件变量,您的生产者可以在数据可用时向您的消费者发出信号以读取内存,反之亦然。代码> 为空。请记住检查虚假唤醒

您需要检查 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 your Consumer to read Memory when data is available, and vice versa when Memory 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文