On MacOSX, in C++, how to do interprocess communication over shared memory without spin lock?
I have two processes:
Producer
and
Consumer
they have a commonly mmaped shared region of memory
Memory
Now, Producer writes stuff to Memory. Consum开发者_如何学编程er 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?
how about using mutexes? since a mutex will sleep until the resource is available, you won't experience the spin-wait problem.
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.
精彩评论