我有两个过程:
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.
我如何实现这一目标?
使用互斥锁怎么样?由于互斥锁会一直休眠直到资源可用,所以您不会遇到自旋等待问题。
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.