source: trunk/doc/src/examples/sharedmemory.qdoc@ 560

Last change on this file since 560 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \example ipc/sharedmemory
44 \title Shared Memory Example
45
46 The Shared Memory example shows how to use the QSharedMemory class
47 to implement inter-process communication using shared memory. To
48 build the example, run make. To run the example, start two instances
49 of the executable. The main() function creates an \l {QApplication}
50 {application} and an instance of our example's Dialog class. The
51 dialog is displayed and then control is passed to the application in
52 the standard way.
53
54 \snippet examples/ipc/sharedmemory/main.cpp 0
55
56 Two instances of class Dialog appear.
57
58 \image sharedmemory-example_1.png Screenshot of the Shared Memory example
59
60 Class Dialog inherits QDialog. It encapsulates the user interface
61 and an instance of QSharedMemory. It also has two public slots,
62 loadFromFile() and loadFromMemory() that correspond to the two
63 buttons on the dialog.
64
65 \snippet examples/ipc/sharedmemory/dialog.h 0
66
67 The constructor builds the user interface widgets and connects the
68 clicked() signal of each button to the corresponding slot function.
69
70 \snippet examples/ipc/sharedmemory/dialog.cpp 0
71
72 Note that "QSharedMemoryExample" is passed to the \l {QSharedMemory}
73 {QSharedMemory()} constructor to be used as the key. This will be
74 used by the system as the identifier of the underlying shared memory
75 segment.
76
77 Click the \tt {Load Image From File...} button on one of the
78 dialogs. The loadFromFile() slot is invoked. First, it tests whether
79 a shared memory segment is already attached to the process. If so,
80 that segment is detached from the process, so we can be assured of
81 starting off the example correctly.
82
83 \snippet examples/ipc/sharedmemory/dialog.cpp 1
84
85 The user is then asked to select an image file using
86 QFileDialog::getOpenFileName(). The selected file is loaded into a
87 QImage. Using a QImage lets us ensure that the selected file is a
88 valid image, and it also allows us to immediately display the image
89 in the dialog using setPixmap().
90
91 Next the image is streamed into a QBuffer using a QDataStream. This
92 gives us the size, which we then use to \l {QSharedMemory::}
93 {create()} our shared memory segment. Creating a shared memory
94 segment automatically \l {QSharedMemory::attach()} {attaches} the
95 segment to the process. Using a QBuffer here lets us get a pointer
96 to the image data, which we then use to do a memcopy() from the
97 QBuffer into the shared memory segment.
98
99 \snippet examples/ipc/sharedmemory/dialog.cpp 2
100
101 Note that we \l {QSharedMemory::} {lock()} the shared memory segment
102 before we copy into it, and we \l {QSharedMemory::} {unlock()} it
103 again immediately after the copy. This ensures we have exclusive
104 access to the shared memory segment to do our memcopy(). If some
105 other process has the segment lock, then our process will block
106 until the lock becomes available.
107
108 Note also that the function does not \l {QSharedMemory::} {detach()}
109 from the shared memory segment after the memcopy() and
110 unlock(). Recall that when the last process detaches from a shared
111 memory segment, the segment is released by the operating
112 system. Since this process only one that is attached to the shared
113 memory segment at the moment, if loadFromFile() detached from the
114 shared memory segment, the segment would be destroyed before we get
115 to the next step.
116
117 When the function returns, if the file you selected was qt.png, your
118 first dialog looks like this.
119
120 \image sharedmemory-example_2.png Screenshot of the Shared Memory example
121
122 In the second dialog, click the \tt {Display Image From Shared
123 Memory} button. The loadFromMemory() slot is invoked. It first \l
124 {QSharedMemory::attach()} {attaches} the process to the same shared
125 memory segment created by the first process. Then it \l
126 {QSharedMemory::lock()} {locks} the segment for exclusive access and
127 links a QBuffer to the image data in the shared memory segment. It
128 then streams the data into a QImage and \l {QSharedMemory::unlock()}
129 {unlocks} the segment.
130
131 \snippet examples/ipc/sharedmemory/dialog.cpp 3
132
133 In this case, the function does \l {QSharedMemory::} {detach()} from
134 the segment, because now we are effectively finished using
135 it. Finally, the QImage is displayed. At this point, both dialogs
136 should be showing the same image. When you close the first dialog,
137 the Dialog destructor calls the QSharedMemory destructor, which
138 detaches from the shared memory segment. Since this is the last
139 process to be detached from the segment, the operating system will
140 now release the shared memory.
141
142 */
Note: See TracBrowser for help on using the repository browser.