I am writing a stochastic cellular automata where I have Cells (defined as a group of pixels) playing on a grid. The grid is in the main program, and I have a class for the Cell object and a class inheriting Thread for the stochastic computation of the Cell behavior. My problem is that when two cells come in contact, they can randomly acquire the same pixel of the grid at the same time, or one can acquire it without the other knowing it yet. So it causes some bugs where pixels are not attributed to the Cell but the Cell still recognizes it as its "self".
My question is: how to synchronize (or "prioritize"?) Threads so that they don't modify the same common component at the same time?
Thank you.