Why is this an issue?
A thread acquiring a lock on an object that can be accessed across application domain boundaries runs the risk of being blocked by another thread
in a different application domain. Objects that can be accessed across application domain boundaries are said to have weak identity. Types with weak
identity are:
-
MarshalByRefObject
-
ExecutionEngineException
-
OutOfMemoryException
-
StackOverflowException
-
String
-
MemberInfo
-
ParameterInfo
-
Thread
Noncompliant code example
public class Sample
{
string myString = "foo";
public void Go()
{
lock (myString) { } // Noncompliant
}
}
Compliant solution
public class Sample
{
private readonly static string thisLock = new object();
public void Go()
{
lock (thisLock) { }
}
}