-2

I want to have two different classes accessing to a shared array with mutex protected. One class writing into the array and the other reading from the array if there is an element in the array. What are the possible ways of such implementation in C++?

Thanks

Avb Avb
  • 545
  • 2
  • 13
  • 25
  • 3
    [producer consumer](https://en.wikipedia.org/wiki/Producer–consumer_problem) please read – aaronman Aug 05 '13 at 20:41
  • Thanks for the link. I just looked at C++ example. In the example, producer and consumer are written in the same file. In my case, producer and consumer are seperate classes. In this case, how can I create a shared variable? – Avb Avb Aug 05 '13 at 20:48

1 Answers1

0

Sounds like a textbook use of mutex. Create an object to store the array and the mutex in private values while having get and set fictions lock at start and unlock before return.

GreenFox
  • 1
  • 1
  • Using mutex is not a problem. My main concernm, how can I create a shared variables between different objects of different classes? – Avb Avb Aug 05 '13 at 20:55
  • Pass a pointer or reference to each object that you want to have access to the shared variables, and have them keep that pointer/reference as a member variable. – Jeremy Friesner Aug 05 '13 at 21:10