Can Q_GLOBAL_STATIC replace KStaticDelete ?

Thiago Macieira thiago at kde.org
Thu Jan 18 19:34:18 GMT 2007


Matthias Kretz wrote:
>On Thursday 18 January 2007 12:13, Thiago Macieira wrote:
>> We cannot also rely on the C++ compiler having guards against
>> reentrancy in static objects.
>
>Meaning we may not rely on
>static QGlobalStatic<TYPE > this_##NAME;
>to be atomic?

QGlobalStatic doesn't do anything. It's just a struct with a destructor.

It's the object creation and assignment that has to be thread-safe. That's 
why we need QAtomic, because the language itself provides no guarantees.

>template <typename T> class KGlobalStatic2
>{
>public:
>    T* pointer;
>    inline KGlobalStatic2() : pointer(new T) {}
>    inline ~KGlobalStatic2()
>    {
>        delete pointer;
>        pointer = 0;
>    }
>};
>
>#define K_GLOBAL_STATIC_THREADSAFE(TYPE, NAME)                         
> \ static TYPE *NAME()                                                 \
> {                                                                   \
> static KGlobalStatic2<TYPE > this_##NAME;                       \
> return this_##NAME.pointer;                                     \ }

Like I said, C++ does not guarantee thread-safety or even reentrancy for 
static object creations. This macro you posted is actually the thread 
unsafe version. For that, it's actually very useful and simple enough to 
be deployed.

-- 
  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: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20070118/3a589f35/attachment.sig>


More information about the kde-core-devel mailing list