|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the documentation of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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 */ |