We all know that the reference type object has two fields in addition to the cost of the instance field: Type pointer and synchronized block index (SYNCBLOCKINDEX). Synchronization Block Index this thing is less attention than its brother type pointer, it seems a bit cold, in fact, this brother is extraordinary, in the CLR is all-powerful, many functions have to use it to achieve.
Next I will use three articles to introduce the synchronization block index. NET.
Since the subtitle of this chapter starts with lock, I'll give some examples of lock:
Code 1
public class Singleton
{
private static object lockHelper = new object();
private static Singleton _instance = null;
public static Singleton Instance
{
get
{
lock (lockHelper)
{
if (_instance == null)
_instance = new Singleton();
}
return _instance;
}
}
}
Code 2
public class Singleton
{
private static Singleton _instance = null;
public static Singleton Instance
{
get
{
object lockHelper = new object();
lock (lockHelper)
{
if (_instance == null)
_instance = new Singleton();
}
return _instance;
}
}
}