Re: Simple and fast Singleton pattern for ya.

From: William Stacey (staceywREMOVE_at_mvps.org)
Date: 02/10/04


Date: Mon, 9 Feb 2004 19:01:07 -0500

Not exactly what happens here. This is not the same as the double checked
issue. DCL uses an unsynchronized testing of the object reference. So the
ref could be assigned by thread1 and then context switch, then thread2 could
get that ref and try to use it before construction. This does not happen
here because construction is completed before the interlock releases the
lock. The actual creation is protected by a memory barrier using the
"lock()". The assignment also creates a "write" barrier on the ref from
doco I have seen. Only then is the flag set atomically. If we want to be
really sure, we could also add a Thread.MemoryBarrier() instruction before
we open the lock like so:
...
Thread.MemoryBarrier();
Interlocked.Exchange(ref created, 1);
...

-- 
William Stacey, MVP
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1a9201aa83d9b45d98a0a2@msnews.microsoft.com...
> William Stacey <staceywREMOVE@mvps.org> wrote:
> > Thanks for seeing that Jon.  There was a small window there.  Here is
the
> > fix to correct for that possibility.
> > We use a lock once, in the case where multiple threads actually try to
get
> > the first instance at the "same" time.
> > The first thread to get the lock, will create the instance.  If multiple
> > threads happen to wait on the lock at same time before the instance is
> > created, they will "wake" up to see instance was created and return it.
Any
> > future threads will not hit the lock, but just do the quick
> > interlocked.compareexchange to test for created.  So except for the
noted
> > exception on first use, no expensive locks are hit.   Cheers!
>
> Nope, you've then got the same problem as the normal double-check lock
> algorithm - you could see the reference to the singleton before it's
> properly initialised.
>
> You basically need a memory barrier there *somewhere*.
>
> -- 
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too


Relevant Pages

  • Re: [patch] aio: fix buggy put_ioctx call in aio_complete
    ... put_ioctx with last ref count on ioctx and trigger a bug warning. ... It abuses rcu over a buggy code. ... I'm worried about longer lock hold time in aio_complete and potentially ...
    (Linux-Kernel)
  • Re: file locking.
    ... What we do is grab an extra ref to each struct file that is dead and then ... we hold the extra file ref for unp sockets that would be closed. ... This portion of the algorithm is not significantly different from before. ... the lock inbetween operations and get an accurate count of how big the ...
    (freebsd-arch)
  • RE : [tipc-discussion] /net/tipc/port.c: Use tipc_port_unlock
    ... The file net/tipc/port.c takes a lock using the ... goto reject; ... ref, unsigne ...
    (Linux-Kernel)
  • Re: D3 b-tree problem
    ... > lock then you could parse the lock table or you could use system level ... > LOCK/UNLOCK for the purpose - more inelegant solutions but feasible. ... >>As for REF I believe that you will find it is not the indexing so much as ... Just put it into the VME. ...
    (comp.databases.pick)
  • Re: [ltt-dev] [RFC git tree] Userspace RCU (urcu) for Linux (repost)
    ... My confusion -- I was testing my old approach where the memory barriers ... I believe you were actually looking for a memory barrier here, ... LOCK operations. ... A LOCK operation should almost always be paired with an UNLOCK operation. ...
    (Linux-Kernel)