Awesome.

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Stoyan Damov
Sent: Monday, July 09, 2007 2:51 AM
To: [email protected]
Subject: Re: [ADVANCED-DOTNET] C# Singleton; foot in mouth

>         protected static I Provider
>         {
>                 if (_provider == null)
>                 {
>                         lock (_syncLock)
>                         {
>                                 if (provder = null)
>                                         provider =
> Activator.createfromconfig(...);
>                         }
>                 }
>                 return _provider;
> }

>
> But, that is a double-check lock, and I would need to document that at
least
> the BaseManager.Provider property is thread safe.
>

Yes, but your DCL is broken. However, if you rewrite it like this:

if (provider_ == null)
{
    lock (syncLock_)
    {
        if (provider_ == null)
        {
            I instance = Activator....
            Thread.MemoryBarrier(); // so that any out-of-order writes
complete[1]
            provider_ = instance;
        }

    }
}

it will become thread-safe.

Cheers,
Stoyan

[1] I think I've read this a couple of years ago in Brad Abrams blog.

===================================
This list is hosted by DevelopMentorR  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to