views:

44

answers:

2

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?

A: 

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

maranas
A: 

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.

Void