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
src_corelib_thread_qmutex.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QMutex>
5#include <QMutexLocker>
6#include <QWaitCondition>
7
8//! [0]
9int number = 6;
10
11void method1()
12{
13 number *= 5;
14 number /= 4;
15}
16
17void method2()
18{
19 number *= 3;
20 number /= 2;
21}
22//! [0]
23
25{
26 //! [1]
27 // method1()
28 number *= 5; // number is now 30
29 number /= 4; // number is now 7
30
31 // method2()
32 number *= 3; // number is now 21
33 number /= 2; // number is now 10
34 //! [1]
35
36
37 //! [2]
38 // Thread 1 calls method1()
39 number *= 5; // number is now 30
40
41 // Thread 2 calls method2().
42 //
43 // Most likely Thread 1 has been put to sleep by the operating
44 // system to allow Thread 2 to run.
45 number *= 3; // number is now 90
46 number /= 2; // number is now 45
47
48 // Thread 1 finishes executing.
49 number /= 4; // number is now 11, instead of 10
50 //! [2]
51}
52
54{
55 //! [3]
57 int number = 6;
58
59 void method1()
60 {
61 mutex.lock();
62 number *= 5;
63 number /= 4;
64 mutex.unlock();
65 }
66
67 void method2()
68 {
69 mutex.lock();
70 number *= 3;
71 number /= 2;
72 mutex.unlock();
73 }
74 //! [3]
75
76 int moreComplexFunction(int flag) { return 0; }
77
78 int anotherFunction() { return 0; }
79
80 //! [4]
81 int complexFunction(int flag)
82 {
83 mutex.lock();
84
85 int retVal = 0;
86
87 switch (flag) {
88 case 0:
89 case 1:
90 retVal = moreComplexFunction(flag);
91 break;
92 case 2:
93 {
94 int status = anotherFunction();
95 if (status < 0) {
96 mutex.unlock();
97 return -2;
98 }
99 retVal = status + flag;
100 }
101 break;
102 default:
103 if (flag > 10) {
104 mutex.unlock();
105 return -1;
106 }
107 break;
108 }
109
110 mutex.unlock();
111 return retVal;
112 }
113 //! [4]
114}
115
117
118int moreComplexFunction(int flag) { return 0; }
119
120int anotherFunction() { return 0; }
121
122//! [5]
123int complexFunction(int flag)
124{
125 QMutexLocker locker(&mutex);
126
127 int retVal = 0;
128
129 switch (flag) {
130 case 0:
131 case 1:
132 return moreComplexFunction(flag);
133 case 2:
134 {
135 int status = anotherFunction();
136 if (status < 0)
137 return -2;
138 retVal = status + flag;
139 }
140 break;
141 default:
142 if (flag > 10)
143 return -1;
144 break;
145 }
146
147 return retVal;
148}
149//! [5]
150
151bool signalled = false;
153
154//! [6]
156{
157private:
158 QMutexLocker<QMutex> locker;
159
160public:
161 SignalWaiter(QMutex *mutex)
162 : locker(mutex)
163 {
164 }
165
167 {
168 //...
169 while (!signalled)
170 waitCondition.wait(locker.mutex());
171 //...
172 }
173};
174//! [6]
bool examples()
[3]
int moreComplexFunction(int flag)
QWaitCondition waitCondition
bool signalled
[5]
int complexFunction(int flag)
[5]
int anotherFunction()