C
Chris Newby
I'm trying to undertand some details on the C# "lock" statement. I came
across some code during a review and thought I spotted a deadlock scenario,
but the programmer said he'd taken it from a reliable source.
Does the following code contain a potential deadlock? or are the locks
granted to syncRoot specific to each method?
==========================================================
private static readonly object syncRoot = new object();
private static object resource;
public static object Resource
{
get
{
if( resource == null )
{
lock( syncRoot )
{
resource = new object();
}
}
return resource;
}
}
public static void DoSomethingToResource()
{
lock( syncRoot )
{
object resourcePlaceHolder = Resource;
}
}
==========================================================
TIA//
across some code during a review and thought I spotted a deadlock scenario,
but the programmer said he'd taken it from a reliable source.
Does the following code contain a potential deadlock? or are the locks
granted to syncRoot specific to each method?
==========================================================
private static readonly object syncRoot = new object();
private static object resource;
public static object Resource
{
get
{
if( resource == null )
{
lock( syncRoot )
{
resource = new object();
}
}
return resource;
}
}
public static void DoSomethingToResource()
{
lock( syncRoot )
{
object resourcePlaceHolder = Resource;
}
}
==========================================================
TIA//