There are 2 classes

class A : class Parent { public void Draw() } class B : class Parent { public void Draw() } 

And you need to synchronize these 2 threads so that the methods are executed strictly in turn. (or at least to the end) The Parent class has a synchronization object. The Draw method is implemented like this.

 public void Draw() { while(true) { lock(obj) { //Рисуем объект } } } 

As it seemed to me, the threads should have been executed, albeit not alternately, but until the end, only after that, another thread had to start (or the same). In fact, it turns out that the processes interfere with each other

 class Parent { public object obj = new object(); } 

Class with threads:

 class Game { public void Start() { A first = new A(); B second = new B(); Thread doRoad = new Thread(A.Draw); Thread doAuto = new Thread(B.Draw); A.Start(); B.Start(); } } 
  • Where are your streams in your code? - VladD
  • And what is obj ? It is important. - VladD
  • In another class. 2 threads are created and start there. - Mikhail Znak
  • Then show this code. The error should be there. - VladD
  • it is a semi-parent class object obj = new object (); - Mikhail Znak

1 answer 1

From the discussion in the comments it turned out that each of the objects has its own copy of obj . Therefore, there is no synchronization: after all, different objects are synchronized, each in its own obj !

Make obj static field of the base class.

  • Yes, exactly, I missed this moment. Thank you very much - Mikhail Znak
  • @MikhailZnak: Please! - VladD