Re: lock(...) question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Alvin Bruney [MVP] (vapor)
Date: 02/02/05


Date: Wed, 2 Feb 2005 13:07:36 -0400


> This will prevent creating instance twice.... Is it clear ?

no, it is not clear. the second thread will simply get a reference to the
singleton object of thread A.
The first null check simply avoids the overhead of a lock as you rightly
pointed out. Otherwise, you have a situation where you lock just to read a
variable that
may or may not be null which is needlessly expensive. You test the reference
again for safety inside the critical section since the examining thread is
the only thread that holds a reference to
the object inside an executing application domain. You would need a stronger
lock (mutex etc) if the singleton reference were exposed to multiple
processes or app domains.

-- 
Regards,
Alvin Bruney [MVP ASP.NET]
[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
----------------------------------------------------------
"Yunus Emre ALPÖZEN" <yemre@msakademik.net> wrote in message 
news:ukMFSRUCFHA.3728@TK2MSFTNGP14.phx.gbl...
> Just think. Two thread wants to get an instance of your singleton class. 
> One thread comes and locks m_syncRoot and starts to create instance. At 
> this time another thread has came and second one also wants to lock. And 
> it will wait for the first thread. After first thread releases m_syncRoot. 
> Second one is allowed to lock m_syncRoot. At that time there is an 
> instance of singleton class.
>
> This will prevent creating instance twice.... Is it clear ?
>
> -- 
> Thanks,
> Yunus Emre ALPÖZEN
>
>
>
> "wh1974" <wayne@pyesmeadow.com> wrote in message 
> news:1107360873.456370.21160@o13g2000cwo.googlegroups.com...
>> 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.
>>
>
> 


Relevant Pages

  • Re: Simple and fast Singleton pattern for ya.
    ... The first thread to get the lock, ... no expensive locks are hit. ... // Vars used by singleton logic. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: "Singleton" Like Pattern Implementation Suggestions Needed
    ... The double check lock should theoretically work on .net and is very easy to ... public sealed class Singleton ... >>> of clients utilizing the same server side components for their processing ... >>> Check Numbers for a given bank account when i'm producing a check document ...
    (microsoft.public.dotnet.distributed_apps)
  • Re: Memory barrier note
    ... with a lock prefix for MP machines. ... A lockwould boil down to the same instruction. ... >> public sealed class Singleton ... >bidirectional memory barrier. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: "Singleton" Like Pattern Implementation Suggestions Needed
    ... You can use a Singleton combined with the Double-Check Lock idiom. ... > Remoting for client to server side communication (server side is where all ... On initialization of the bank account ...
    (microsoft.public.dotnet.distributed_apps)
  • Re: Deletion of a COM object in VC6.0 vs VC7.1
    ... The class factory only ... >creates a singleton object when it is requested for the first time, ... >holds an AddRef'ed reference to it. ... >why the singleton is destroyed prematurely. ...
    (microsoft.public.vc.atl)