Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #1332

Re: Double check locking - are release and acquire fences enough for C++ memory model?

From Gerhard Fiedler <gelists@gmail.com>
Subject Re: Double check locking - are release and acquire fences enough for C++ memory model?
Newsgroups comp.programming.threads
References <9b219fb9-0b51-4c06-9398-d1e7ab878f7d@googlegroups.com> <511207c5$0$6565$9b4e6d93@newsspool3.arcor-online.net>
Date 2013-02-06 11:58 -0200
Message-ID <1qaaqol9bs9d7.dlg@gelists.gmail.com> (permalink)
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


Marcel Müller wrote:

> static T* ptr = NULL;
> static Mutex mtx;
> 
> T* get_singleton_T()
> { if (!ptr)          // First check, race condition accepted
>    { mtx.lock();      // wait for mutex, full mem bar
>      if (!ptr)        // the double check
>        ptr = new T(); // create singleton
>      mtx.release();   // release mutex, full mem bar
>    }
>    return ptr;
> }
> 
> With this code a dependency of singleton X to singleton Y is no problem 
> at all, since the getter will never return incomplete objects.

A side question: Isn't this a broken implementation? Is it not possible
that ptr is assigned the address of the uninitialized memory for T
before T is initialized -- which then could lead to returning
uninitialized memory in a second thread?

Gerhard

Back to comp.programming.threads | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Double check locking - are release and acquire fences enough for C++ memory model? Michael Podolsky <michael.podolsky.69@gmail.com> - 2013-02-04 23:09 -0800
  Re: Double check locking - are release and acquire fences enough for C++ memory model? Marcel Müller <news.5.maazl@spamgourmet.org> - 2013-02-06 08:35 +0100
    Re: Double check locking - are release and acquire fences enough for C++ memory model? Michael Podolsky <michael.podolsky.69@gmail.com> - 2013-02-06 04:40 -0800
      Re: Double check locking - are release and acquire fences enough for C++ memory model? Marcel Müller <news.5.maazl@spamgourmet.org> - 2013-02-06 14:39 +0100
        Re: Double check locking - are release and acquire fences enough for C++ memory model? Michael Podolsky <michael.podolsky.69@gmail.com> - 2013-02-06 19:19 -0800
    Re: Double check locking - are release and acquire fences enough for C++ memory model? Gerhard Fiedler <gelists@gmail.com> - 2013-02-06 11:58 -0200
  Re: Double check locking - are release and acquire fences enough for C++ memory model? Michael Podolsky <michael.podolsky.rrr@gmail.com> - 2013-04-01 12:12 -0700

csiph-web