[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: Misuse of K_GLOBAL_STATIC?
From:       Johannes Sixt <j.sixt () viscovery ! net>
Date:       2008-05-14 15:53:28
Message-ID: 482B0AF8.1090905 () viscovery ! net
[Download RAW message or body]

Thiago Macieira schrieb:
> KIMProxy *KIMProxy::instance()
> {
>     static KIMProxy *self;
>     Q_ONCE 
>         self = new KIMProxy;
>     return self;
> }
> 
> Unlike the global statics, that ensures the "new" call happens exactly once 
> (unless an exception is thrown). With the global statics, in the event of a 
> reentrancy, the static could be built more than once, even though only one 
> copy will survive.

In the light of this paper:

http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf

I'd be interested how Q_ONCE is implemented, in particular, whether it
uses the Double-Checked-Locking pattern.

[Summary: DCL tries to avoid locking the mutex if the object was already
allocated. The paper argues that DCL is unsafe because the compiler is
allowed to rearrange instructions in so many ways.]

-- Hannes
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic