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

Last change on this file since 1168 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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