I’m reviewing some code, and saw something I have never seen before. I did a find all references on an object used for locking, and got the following results.
I have replaced all of the file/class/member names to protect those who may need protecting.
C:\Sln1\ProjX\ClassX.cs - (17, 26) : public static object PublicStaticLockObj = new object();
C:\Sln1\Proj1\Class1.cs - (193, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj1\Class1.cs - (228, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj1\Class1.cs - (92, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj2\Class2.cs - (115, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj2\Class2.cs - (181, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj2\Class2.cs - (216, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj3\Class3.cs - (160, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj3\Class3.cs - (195, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj3\Class3.cs - (95, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj4\Class4.cs - (133, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj4\Class4.cs - (252, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj4\Class4.cs - (286, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj5\Class5.cs - (252, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj5\Class5.cs - (320, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj5\Class5.cs - (360, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj6\Class6.cs - (112, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj6\Class6.cs - (177, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj6\Class6.cs - (212, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj7\Class7.cs - (165, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj8\Class8.cs - (129, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj8\Class8.cs - (198, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj8\Class8.cs - (233, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj9\Class9.cs - (156, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj9\Class9.cs - (191, 20) : lock (ClassX.PublicStaticLockObj)
C:\Sln1\Proj9\Class9.cs - (90, 20) : lock (ClassX.PublicStaticLockObj)
Is there ever a condition where sharing a lock across modules is a good way to solve a problem? Is there any guidance on locking on public objects?
If code smells, that reeks.
The main weakness of locks as a synchronization mechanism is that they are not composeable – needing to acquire multiple locks very easily causes deadlock.
The only way to prevent deadlocks is to limit the circumstances in which you will acquire a lock to only where it is absolutely necessary, and also limit the actions you will perform while the lock is held, e.g. avoid doing anything that can possibly take another lock, but also avoid doing anything that can block, sleep, or take a long time, since whatever operation takes so long has now effectively been promoted – it can block multiple threads at once!
Making locks public is a way to encourage developers to take locks they really don’t need to take, or that they really haven’t considered the consequences of taking.
You probably want a more official reference than me! Try the ‘lock’ statement documentation.
Source: http://msdn.microsoft.com/en-us/library/c5kehkcz(v=vs.80).aspx