Kernel objects
.NET abstractions
WaitHandle
Mutual Exclusion
Multiple can access critical region
'Safe' example
public class Manager
{
public void SuperSafeMethod()
{
object locker = new object();
lock (locker)
{
}
}
}
'Safe' example
public class Manager
{
private double penetration;
public void Increment()
{
lock((object)penetration)
{
penetration++;
}
}
}
lock - pre .NET 4.0
Monitor.Enter(locker);
try
{
// access to shared state
}
finally
{
Monitor.Exit(locker);
}
lock - post .NET 4.0
bool lockTaken;
try
{
Monitor.Enter(locker, out lockTaken)
// access to shared state
}
finally
{
if(lockTaken)
{
Monitor.Exit(locker);
}
}
Thin lock
Sygnaling
public class Manager
{
[MethodImplAttribute(MethodImplOptions.Synchronized)]
public void Increment(){ }
}
Why?