Thiago Macieira thiago at kde.org
Wed May 14 13:11:38 BST 2008

On Wednesday 14 May 2008 13:40:03 Jaroslaw Staniek wrote:
> Thiago Macieira said the following, On 2008-05-14 13:28:
> > On Wednesday 14 May 2008 13:21:47 Jarosław Staniek wrote:
> >> Hi, shouldn't we move K_GLOBAL_STATIC out of the bodies in such cases?
> >>
> >>
> >> KIMProxy * KIMProxy::instance()
> >> {
> >> 	K_GLOBAL_STATIC(KIMProxy, s_instance)
> >> 	return s_instance;
> >> }
> >
> > Yes, they should be out of function bodies. The use above is not what
> > K_GLOBAL_STATIC was intended to be. I recommend not doing it even though
> > it works. Something might change in the future.
> If so, please read my other post in this thread: we have many private
> ctors...

I want to publish a thread-safe QSingleton class with Qt 4.5 that would 
alleviate that issue.

The above code could also be re-written as:

KIMProxy *KIMProxy::instance()
    static KIMProxy *self;
        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.

That is supposed to match the code:
KIMProxy *KIMProxy::instance()
    static KIMProxy *self = new KIMProxy;
    return self;

that works perfectly fine in gcc, but whose behaviour is left undefined by the 
C++ standard. (thread-safe statics)
  Thiago Macieira  -  thiago (AT) macieira.info - thiago (AT) kde.org
    PGP/GPG: 0x6EF45358; fingerprint:
    E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20080514/5ca17c75/attachment.sig>

More information about the kde-core-devel mailing list