Smell the sync code?

I am analyzing a lot of multithreaded code, and I see so many locks. Some methods will have two locks per line as follows:

ClassA::foo()
{
   lockA.lock();
   lockB.lock();

   ...//do some stuff

   lockB.unlock();
   lockA.unlock();
}

My question is very general (and I cannot provide valid code). Is that the smell of code? Is this generally bad practice? I cannot prove that the code can be simplified, but it seems that if it were correctly encoded, there would be no need to block two things. A lock should manage a set of resources that need synchronization, right? If there are two locks that overlap resource management, then there might be some locking issues?

Please let me know if you have any understanding.

Thanks JBU

+3
source share
2 answers

, , . , 5 , , , - . 1:1 .

(.. , ). Rezist, , , .

, , , -, , , . , 10 , , , .

, : A ? , , - . - - / .

:

lockA.lock();
//some code
lockA.unlock();
lockB.lock();
//some other code
lockB.unlock();

, : / (, lockA > lockB) .

lockA.lock();
lockB.lock();

lockB.lock();
lockA.lock();
+3

, . , , , , . , , , .

, , - , RAII, (, ).

+2

Source: https://habr.com/ru/post/1787738/


All Articles