Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5
int
number
= 6;
6
7
void
method1
()
8
{
9
number
*= 5;
10
number
/= 4;
11
}
12
13
void
method2
()
14
{
15
number
*= 3;
16
number
/= 2;
17
}
19
20
22
// method1()
23
number
*= 5;
// number is now 30
24
number
/= 4;
// number is now 7
25
26
// method2()
27
number
*= 3;
// number is now 21
28
number
/= 2;
// number is now 10
30
31
33
// Thread 1 calls method1()
34
number
*= 5;
// number is now 30
35
36
// Thread 2 calls method2().
37
//
38
// Most likely Thread 1 has been put to sleep by the operating
39
// system to allow Thread 2 to run.
40
number
*= 3;
// number is now 90
41
number
/= 2;
// number is now 45
42
43
// Thread 1 finishes executing.
44
number
/= 4;
// number is now 11, instead of 10
46
47
49
QMutex
mutex
;
50
int
number
= 6;
51
52
void
method1
()
53
{
54
mutex
.
lock
();
55
number
*= 5;
56
number
/= 4;
57
mutex
.
unlock
();
58
}
59
60
void
method2
()
61
{
62
mutex
.
lock
();
63
number
*= 3;
64
number
/= 2;
65
mutex
.
unlock
();
66
}
68
69
71
int
complexFunction
(
int
flag)
72
{
73
mutex
.
lock
();
74
75
int
retVal = 0;
76
77
switch
(flag) {
78
case
0:
79
case
1:
80
retVal = moreComplexFunction(flag);
81
break
;
82
case
2:
83
{
84
int
status = anotherFunction();
85
if
(status < 0) {
86
mutex
.
unlock
();
87
return
-2;
88
}
89
retVal = status + flag;
90
}
91
break
;
92
default
:
93
if
(flag > 10) {
94
mutex
.
unlock
();
95
return
-1;
96
}
97
break
;
98
}
99
100
mutex
.
unlock
();
101
return
retVal;
102
}
104
105
107
int
complexFunction
(
int
flag)
108
{
109
QMutexLocker
locker(&
mutex
);
110
111
int
retVal = 0;
112
113
switch
(flag) {
114
case
0:
115
case
1:
116
return
moreComplexFunction(flag);
117
case
2:
118
{
119
int
status = anotherFunction();
120
if
(status < 0)
121
return
-2;
122
retVal = status + flag;
123
}
124
break
;
125
default
:
126
if
(flag > 10)
127
return
-1;
128
break
;
129
}
130
131
return
retVal;
132
}
134
135
137
class
SignalWaiter
138
{
139
private
:
140
QMutexLocker<QMutex> locker;
141
142
public
:
143
SignalWaiter
(
QMutex
*
mutex
)
144
: locker(
mutex
)
145
{
146
}
147
148
void
waitForSignal
()
149
{
150
...
151
while
(!signalled)
152
waitCondition.wait(locker.mutex());
153
...
154
}
155
};
QMutexLocker
\inmodule QtCore
Definition
qmutex.h:313
QMutex
\inmodule QtCore
Definition
qmutex.h:281
QMutex::unlock
void unlock() noexcept
Unlocks the mutex.
Definition
qmutex.h:289
QMutex::lock
void lock() noexcept
Locks the mutex.
Definition
qmutex.h:286
SignalWaiter
[5]
Definition
src_corelib_thread_qmutex.cpp:138
SignalWaiter::waitForSignal
void waitForSignal()
Definition
src_corelib_thread_qmutex.cpp:148
SignalWaiter::SignalWaiter
SignalWaiter(QMutex *mutex)
Definition
src_corelib_thread_qmutex.cpp:143
complexFunction
void complexFunction()
[1]
Definition
ntfsp.cpp:15
method2
void method2()
Definition
src_corelib_thread_qmutex.cpp:13
mutex
QMutex mutex
[2]
Definition
src_corelib_thread_qmutex.cpp:49
number
int number
[0]
Definition
src_corelib_thread_qmutex.cpp:5
method1
void method1()
Definition
src_corelib_thread_qmutex.cpp:7
qtbase
src
corelib
doc
snippets
code
src_corelib_thread_qmutex.cpp
Generated by
1.10.0