Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qapplicationstatic.qdoc
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \macro Q_APPLICATION_STATIC(Type, VariableName, ...)
6 \since 6.3
7 \relates QGlobalStatic
8
9 This macro extends Q_GLOBAL_STATIC and creates a global and static object
10 of type \l QGlobalStatic, of name \a VariableName, initialized by the
11 variadic arguments, and that behaves as a pointer to \a Type, where the
12 actual lifetime of the type is bound to the QCoreApplication. The object
13 created by Q_APPLICATION_STATIC initializes itself on the first use, which
14 means that it will not increase the application or the library's load time.
15 Additionally, the object is initialized in a thread-safe manner on all
16 platforms.
17
18 In contrast to Q_GLOBAL_STATIC where the type is only meant to be destroyed at
19 program exit, here the actual lifetime of the type is bound to the lifetime of
20 the QCoreApplication. This makes it ideal to store semi-static QObjects, which
21 should also be destroyed once the QCoreApplication is destroyed. This means the
22 type will get deleted once the QCoreApplication emits the destroyed signal.
23 It is permitted for the object to be recreated when it's accessed again, if
24 a new QCoreApplication has also been created.
25
26 Since the value is bound to the QCoreApplication, it should only ever be
27 accessed if there is a valid QCoreApplication::instance(). Accessing this
28 object before QCoreApplication is created or after it's destroyed will
29 produce warnings and may have unpredictable behavior.
30
31 The typical use of this macro is as follows, in a global context (that is,
32 outside of any function bodies):
33
34 \code
35 Q_APPLICATION_STATIC(MyQObjectType, staticType, "some string", function())
36 \endcode
37
38 Do note that the arguments passed in variadic fashion to this macro are
39 evaluated every time the object is constructed, so in the above example,
40 the function \c{function} will be called more than once if the object is
41 recreated.
42
43 Aside from the value also being bound to the lifetime of the QCoreApplication,
44 this macro behaves identically to Q_GLOBAL_STATIC(). Please see that macro's
45 documentation for more information.
46
47 \section1 Threading guarantees
48
49 The Q_APPLICATION_STATIC macro ensures that the object is initialized only
50 once (per lifetime of a QCoreApplication), even if multiple threads try to
51 concurrently access the object. This is done by providing a per-object
52 mutex; application and library developers need to be aware that their
53 object will be constructed with this mutex locked and therefore must not
54 reenter the same object's initialization, or a deadlock will occur.
55
56 There is no thread-safety on the destruction of the object: user code must
57 not access this object once the QCoreApplication destructor starts to run.
58 User code must arrange to ensure this does not happen, such as by not
59 accessing it once the main thread's event loop has exited.
60
61 Like Q_GLOBAL_STATIC, Q_APPLICATION_STATIC provides no thread-safety
62 guarantees for accesses to the object once creation is finished. It is up
63 to user code to ensure that no racy data accesses happen.
64
65 In case the object created by this operation is a QObject, its associated
66 thread will be the one that succeeded in creating it. It will be destroyed
67 by the main thread, so a \l{QObject::}{moveToThread()} to the main thread
68 or to no thread before destruction is adviseable. Doing so from the
69 constructor of the class in question is a sensible solution if one can't
70 guarantee that the main thread will be the one to initialize the object.
71
72 \omit
73 \section1 Implementation details
74 See \l Q_GLOBAL_STATIC implementation details for an introduction.
75
76 Q_APPLICATION_STATIC uses the same \l QGlobalStatic public class that
77 Q_GLOBAL_STATIC does, but instead uses a QtGlobalStatic::ApplicationHolder
78 template class as the template parameter. The differences to
79 QtGlobalStatic::Holder are:
80
81 \list
82 \li The ApplicationHolder class is empty. Unlike Holder, the storage is
83 provided as a \c {static inline} member, simply so that the static
84 member reset() function can access it without having to save the
85 pointer in a lambda.
86
87 \li The ApplicationHolder constructor is trivial; initialization of the
88 type is instead deferred to the \c pointer() function. This means the
89 C++11 thread-safe initialization of statics does not protect the
90 object.
91
92 \li Instead, ApplicationHolder provides a mutex (implemented as a \c
93 {static inline} member of type \l QBasicMutex) and locks it before
94 constructing or destructing the object.
95
96 \li After constructing the object, it will QObject::connect() the
97 QCoreApplication::destroyed() signal to a function that will in turn
98 destroy the object.
99
100 \li The destructor will destroy the object if the application is
101 exiting without first destroying the QCoreApplication object (i.e., a
102 call to \c ::exit) or this Q_APPLICATION_STATIC is part of a plugin
103 that is being unloaded.
104 \endlist
105
106 \endomit
107
108 \sa Q_GLOBAL_STATIC, QGlobalStatic
109*/