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
, , . , 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();
, . , , , , . , , , .
, , - , RAII, (, ).
Source: https://habr.com/ru/post/1787738/More articles:NSURLCache bypasses HTML5 video tag - html5ASP.NET management flaw - asp.netdjango SelectDateWidget Years in reverse order - djangoWhat is C # the most efficient Delphi TStringList analogue? - objectHas Microsoft changed the way a string is passed as a parameter in ASP.NET MVC 3? - asp.net-mvc-3problem with primerfaces gmap tag - primefacesHow to write a query to remove duplicates in a table? - sqlHow to get the ratio of the width x height of the image on the disk / file? - c #Storing NULL instead of empty row columns - .netHTML source displayed in Firebug in standard browser "View Source"? - domAll Articles