Misuse of K_GLOBAL_STATIC?

Johannes Sixt j.sixt at viscovery.net
Wed May 14 16:53:28 BST 2008


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




More information about the kde-core-devel mailing list