W
wh1974
Just wondering whether somebody could clarify my understanding on some
code I've seen several times over the past week:
if (m_instance == null)
{
lock (m_syncRoot)
{
if (m_instance == null)
{
m_instance = new Singleton();
}
}
}
I have no problem understanding what the code is doing but I fail to
understand the reason for comparing m_instance to null twice.
Surely the following is equivalent:
lock (m_syncRoot)
{
if (m_instance == null)
{
m_instance = new Singleton();
}
}
Is it just a case of avoiding the call to lock(...) to improve
performance?
Thanks,
Wayne.
code I've seen several times over the past week:
if (m_instance == null)
{
lock (m_syncRoot)
{
if (m_instance == null)
{
m_instance = new Singleton();
}
}
}
I have no problem understanding what the code is doing but I fail to
understand the reason for comparing m_instance to null twice.
Surely the following is equivalent:
lock (m_syncRoot)
{
if (m_instance == null)
{
m_instance = new Singleton();
}
}
Is it just a case of avoiding the call to lock(...) to improve
performance?
Thanks,
Wayne.