1

Topic: Question about synchronized block

Whether tell, please, correctly I understand that in Java is connected to each object mutex and when we write SomeClass obj = new SomeClass ();//... synchronized (obj) {obj.someMethod (value1, value2); obj.setSomeValue (value3);} it as a matter of fact is equivalent to such code on a C ++ class SomeClass_withMutex {SomeClass m_obj;//obj std:: mutex m_mutex;//mutex for protecting obj public: void synchMethod (int value1, string value2, double value3) {std:: lock_guard <mutex> locker (m_mutex); m_obj.someMethod (value1, value2); m_obj.setSomeValue (value3);}}; I.e. on pluses we hands connect to object , and in Jav it becomes for all objects language Java and synchronized block is simple in the beginning the unit calls mutex.lock (), and in block end calls mutex.unlock ()?

2

Re: Question about synchronized block

Hello, Maxim Rogozhin, you wrote: > I.e. on pluses we hands connect to object , and in Jav it becomes for all objects language Java and synchronized block is simple in the beginning the unit calls mutex.lock (), and in block end calls mutex.unlock ()? The behavior is similar. In java synchronized still adds memory barrier. I not in course of singularities  , can ReentrantLock be more similar.

3

Re: Question about synchronized block

Hello, Maxim Rogozhin, you wrote: > I.e. on pluses we hands connect to object , and in Jav it becomes for all objects language Java and synchronized block is simple in the beginning the unit calls mutex.lock (), and in block end calls mutex.unlock ()? On pluses if you inside synchMethod recursively will cause it, accordingly once again will try to make mutex.lock (), it is natural in the same flow - problems a case will not be? For at the moment of a call to it anybody unlock did not do, accordingly I assume that will be  out of the blue. On Java if you captured the monitor (over what you do synchronized is is called monitor) - through it perfectly all transits that in the same flow. As here correctly noted are not that other, as ReentrantLock.

4

Re: Question about synchronized block

> I.e. on pluses we hands connect to object , and in Jav it becomes for all objects language Java and synchronized block is simple in the beginning the unit calls mutex.lock (), and in block end calls mutex.unlock ()? In Java each object has a title, and in it title there is a field responsible for lock. For optimization, this field has different sense for cases "", "is locked by one flow" and "two or more flows try to lock" https://wiki.openjdk.java.net/display/H … ronization