Suggestions on how to implement this?

I'm writing code in C++, and I have 3 classes. 1) Memory 2) Producer 3) Consumer.

The Memory class has N blocks of memory; it provides a public interface for Read() and Write() blocks of data. It's thread-safe because of mutex.

The Producer class is a thread, will keep Write() data to different blocks of memory, and for each write, it will command the Consumer to Read() those blocks. The command can be queued up in case Consumer is not done Read() the current block.

The Consumer class is a thread, and will wait for commands from Producer to start Read(), and will keep Read() until the queue is empty.

Because of the above behavior, Producer and Consumer share the same Memory. My question is, how do you make Memory shared by Producer and Consumer? Currently, I make both Producer and Consumer has a private member that is a Memory* pointer. I create a Memory object, then I pass the reference of that object to both Producer and Consumer.

I dont know if this is the best thing. This approach can get messy if I have more objects that need to be shared among the classes. And if I were to bundle up all of the shared objects together into a single class, and pass the composite class to all threads, then it's possible that I pass some objects that aren't necessarily needed by some threads (i.e. if I were to add another thread call "Processor" and maybe it doesnt need "Memory")

Will a Singleton pattern be better? How do developers elegantly "connect up" shared resources among different class/threads? Or am I just thinking too much?
[1619 byte] By [ShaChris23] at [2007-11-20 11:47:40]