In most codes that I wrote or looked at, locking is done using composition, where the class owns a critical sector or mutex:
class MyClass { Mutex mMutex; };
and when mutable members can be accessed through multiple threads, we get and release the lock via RAII as follows:
void MyClass::Method() { Lock lock(mMutex); // ... }
Today I looked at the code in which the code was blocking through inheritance, for example:
class MyClass : public Mutex { // ... };
And blocking is done using the "lock" of the class itself:
void MyClass::Method() { Lock lock(this); // ... }
Are there any advantages or disadvantages to this approach? Or is it just a style issue?
, - . , Mutex (, Mutex , ), , , , (, , Mutex ).
Mutex
, , - , , - , MyClass Mutex. , , . Mutex ( ), , .
MyClass
, , , .NET, "". , .NET lock(this), , , .
lock(this)
. MyClass - , Mutex? , , MyClass Mutex ( MyClass to Mutex "is-a" ).
:
MyClass x; Lock lock(x); x.Method(); // uh oh.
Source: https://habr.com/ru/post/1779904/More articles:SQL Server Cluster Failure Programming - .netSilverlight Drill Down Custom Charts - silverlightGetting delphi color values ββfor Excel lines - colorsHow to play local mp3 files using MediaElement - c #Combination of keys, keystrokes and onblur events in jquery - jqueryText area, nl2br, line breaks galore - htmlDjango Apache Hello World - djangoRTP / TCP / AVP video stream playback on Android - androidHow can I combine two mp3 files with different transfer rates - linuxAttempting to get the value of an element or itself inside any element in Json returns null - javaAll Articles