On Mon, 9 Jul 2007 10:50:31 +0300, Stoyan Damov <[EMAIL PROTECTED]>
wrote:
>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;
>        }
>
>    }
>}

If provider_ is only set in that one place, or if it's set elsewhere and
within(syncLock_) you don't need the MemoryBarrier.  Enter guarantees
acquire semantics and does the memory barrier for you (at the start of the
lock, and since nothing else can access provider while in the lock,
there's no need for the MemoryBarrier).

The only thing that isn't clear (which I should mention is an opinion not
shared by some others) is how the JIT can optimize the write to
provider_.  In which case making provider_ volatile would cause the JIT
not to optimize provider_.

===================================
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