Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
implicit-sharing.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/* TODO: Move some of the documentation from QSharedDataPointer into this
5
document. */
6
7
/*!
8
\group shared
9
\brief How to maximize resource usage by implicit data sharing.
10
\title Implicitly Shared Classes
11
12
These \l{Qt Core} classes provides a safe and efficient way of sharing and
13
manipulating data by \l{Implicit Sharing}{implicitly sharing} data.
14
15
*/
16
17
/*!
18
\page implicit-sharing.html
19
\title Implicit Sharing
20
\ingroup qt-basic-concepts
21
22
\brief Reference counting for fast copying.
23
24
\keyword implicit data sharing
25
\keyword implicit sharing
26
\keyword implicitly shared
27
\keyword reference counting
28
\keyword shared implicitly
29
\keyword shared classes
30
31
Many C++ classes in Qt use implicit data sharing to maximize
32
resource usage and minimize copying. Implicitly shared classes are
33
both safe and efficient when passed as arguments, because only a
34
pointer to the data is passed around, and the data is copied only
35
if and when a function writes to it, i.e., \e {copy-on-write}.
36
37
\section1 Overview
38
39
A shared class consists of a pointer to a shared data block that
40
contains a reference count and the data.
41
42
When a shared object is created, it sets the reference count to 1. The
43
reference count is incremented whenever a new object references the
44
shared data, and decremented when the object dereferences the shared
45
data. The shared data is deleted when the reference count becomes
46
zero.
47
48
\target deep copy
49
\target shallow copy
50
51
When dealing with shared objects, there are two ways of copying an
52
object. We usually speak about \e deep and \e shallow copies. A deep
53
copy implies duplicating an object. A shallow copy is a reference
54
copy, i.e. just a pointer to a shared data block. Making a deep copy
55
can be expensive in terms of memory and CPU. Making a shallow copy is
56
very fast, because it only involves setting a pointer and incrementing
57
the reference count.
58
59
Object assignment (with operator=()) for implicitly shared objects is
60
implemented using shallow copies.
61
62
The benefit of sharing is that a program does not need to duplicate
63
data unnecessarily, which results in lower memory use and less copying
64
of data. Objects can easily be assigned, sent as function arguments,
65
and returned from functions.
66
67
Implicit sharing mostly takes place behind the scenes;
68
the programmer rarely needs to worry about it. However, Qt's
69
container iterators have different behavior than those from
70
the STL. Read \l{Implicit sharing iterator problem}.
71
72
In multithreaded applications, implicit sharing takes place, as explained in
73
\l{Thread-Support in Qt Modules#Threads and Implicitly Shared Classes}
74
{Threads and Implicitly Shared Classes}.
75
76
When implementing your own implicitly shared classes, use the
77
QSharedData and QSharedDataPointer classes.
78
79
\section1 Implicit Sharing in Detail
80
81
Implicit sharing automatically detaches the object from a shared
82
block if the object is about to change and the reference count is
83
greater than one. (This is often called \e {copy-on-write} or
84
\e {value semantics}.)
85
86
An implicitly shared class has control of its internal data. In
87
any member functions that modify its data, it automatically detaches
88
before modifying the data. Notice, however, the special case with
89
container iterators; see \l{Implicit sharing iterator problem}.
90
91
The QPen class, which uses implicit sharing, detaches from the shared
92
data in all member functions that change the internal data.
93
94
Code fragment:
95
\quotefromfile ../gui/painting/qpen.cpp
96
97
\skipuntil void QPen::setCapStyle
98
99
\skipto void QPen::setCapStyle
100
\printuntil }
101
102
\codeline
103
104
\quotefromfile ../gui/painting/qpen.cpp
105
106
\skipuntil void QPen::detach
107
108
\skipto void QPen::detach
109
\printuntil }
110
111
\section1 List of Classes
112
113
The classes listed below automatically detach from common data if
114
an object is about to be changed. The programmer will not even
115
notice that the objects are shared. Thus you should treat
116
separate instances of them as separate objects. They will always
117
behave as separate objects but with the added benefit of sharing
118
data whenever possible. For this reason, you can pass instances
119
of these classes as arguments to functions by value without
120
concern for the copying overhead.
121
122
Example:
123
\snippet code/doc_src_groups.cpp 1
124
125
In this example, \c p1 and \c p2 share data until QPainter::begin()
126
is called for \c p2, because painting a pixmap will modify it.
127
128
\warning Be careful with copying an implicitly shared container
129
(QMap, QList, etc.) while you use
130
\l{STL-style iterators}{STL-style iterator}. See \l{Implicit sharing iterator problem}.
131
132
\target implicitly shared classes
133
\annotatedlist shared
134
*/
qtbase
src
corelib
doc
src
implicit-sharing.qdoc
Generated on
for Qt by
1.14.0