lock try/finally, Monitor.Enter/Monitor.Exit.
, ( VS2015 Preview), , .
var testVar = new List<int>();
lock (testVar)
{
testVar = new List<int>();
testVar.Add(1);
}
:
List<int> list2;
List<int> list = new List<int>();
bool lockTaken = false;
try
{
list2 = list;
Monitor.Enter(list2, ref lockTaken);
list = new List<int> { 1 };
}
finally
{
if (lockTaken)
{
Monitor.Exit(list2);
}
}
, , testVar , list list2. :
list2 list, List<int>.Monitor.Enter(list2, ref lockTaken) List<int> .list List<int>, list2 , .list2
, , , . , , .