views:

544

answers:

1

I've got a producer and a consumer. The producer writes fixed size items on a given shared memory area, and the consumer retrieves them.

The producer can be noticeably slower or faster than the consumer, randomly.

What we want is that

  1. If the producer is running faster than the consumer, when it fills the circular buffer, it keeps writing on the oldest frames (other than the one that the consumer is consuming, of course - I stress this point, producer and consumer must be synchronized in the solution, because they are unrelated processes).

  2. If, instead, the consumer is faster than the producer, it must wait for a new frame and consume it when it's there.

I found implementations of producer/consumers with circular buffers, but only ones that didn't respect the first request (ie, if the circular buffer is full, they wait for the consumer to finish, while what I want is to overwrite the oldest frames).

I'd prefer not to roll my own (prone to bugs) solution, but use a pre-canned, tested one. Can someone point me to a good C implementation? (C++ is also ok).

Many thanks.

A: 

Sounds like you want to use a double ended queue which is provided in C++'s Standard Template Library (STL). But that uses C++, not C. If your producer and consumer are different threads then you'll also need a mutex to protect the queue structure.

progrmr