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?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news.astraweb.com!border6.newsrouter.astraweb.com!not-for-mail
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
User-Agent 40tude_Dialog/2.0.15.84
MIME-Version 1.0
Content-Type text/plain; charset="iso-8859-1"
Content-Transfer-Encoding 8bit
References <9b219fb9-0b51-4c06-9398-d1e7ab878f7d@googlegroups.com> <511207c5$0$6565$9b4e6d93@newsspool3.arcor-online.net>
Date Wed, 6 Feb 2013 11:58:41 -0200
Message-ID <1qaaqol9bs9d7.dlg@gelists.gmail.com> (permalink)
Lines 24
Organization Unlimited download news at news.astraweb.com
NNTP-Posting-Host 9106f9f3.news.astraweb.com
X-Trace DXC=lCRWaNj:nSndoV@f]470JnL?0kYOcDh@jEE63Zc8>Ueg[ln[<B]GNZ`58Z8MMN8fYm:ci1X<GUTBbQUiKf6Cllme]]CGaC[cTGfYX6aP1=]DLh
Xref csiph.com comp.programming.threads:1332

Show key headers only | 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