84fe348eb24bf21733715ff54b52f0ee6cbd7f61
[profile/ivi/qtbase.git] / doc / src / snippets / code / src_corelib_thread_qmutex.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 **     the names of its contributors may be used to endorse or promote
22 **     products derived from this software without specific prior written
23 **     permission.
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 //! [0]
42 int number = 6;
43
44 void method1()
45 {
46     number *= 5;
47     number /= 4;
48 }
49
50 void method2()
51 {
52     number *= 3;
53     number /= 2;
54 }
55 //! [0]
56
57
58 //! [1]
59 // method1()
60 number *= 5;        // number is now 30
61 number /= 4;        // number is now 7
62
63 // method2()
64 number *= 3;        // number is now 21
65 number /= 2;        // number is now 10
66 //! [1]
67
68
69 //! [2]
70 // Thread 1 calls method1()
71 number *= 5;        // number is now 30
72
73 // Thread 2 calls method2().
74 //
75 // Most likely Thread 1 has been put to sleep by the operating
76 // system to allow Thread 2 to run.
77 number *= 3;        // number is now 90
78 number /= 2;        // number is now 45
79
80 // Thread 1 finishes executing.
81 number /= 4;        // number is now 11, instead of 10
82 //! [2]
83
84
85 //! [3]
86 QMutex mutex;
87 int number = 6;
88
89 void method1()
90 {
91     mutex.lock();
92     number *= 5;
93     number /= 4;
94     mutex.unlock();
95 }
96
97 void method2()
98 {
99     mutex.lock();
100     number *= 3;
101     number /= 2;
102     mutex.unlock();
103 }
104 //! [3]
105
106
107 //! [4]
108 int complexFunction(int flag)
109 {
110     mutex.lock();
111
112     int retVal = 0;
113
114     switch (flag) {
115     case 0:
116     case 1:
117         mutex.unlock();
118         return moreComplexFunction(flag);
119     case 2:
120         {
121             int status = anotherFunction();
122             if (status < 0) {
123                 mutex.unlock();
124                 return -2;
125             }
126             retVal = status + flag;
127         }
128         break;
129     default:
130         if (flag > 10) {
131             mutex.unlock();
132             return -1;
133         }
134         break;
135     }
136
137     mutex.unlock();
138     return retVal;
139 }
140 //! [4]
141
142
143 //! [5]
144 int complexFunction(int flag)
145 {
146     QMutexLocker locker(&mutex);
147
148     int retVal = 0;
149
150     switch (flag) {
151     case 0:
152     case 1:
153         return moreComplexFunction(flag);
154     case 2:
155         {
156             int status = anotherFunction();
157             if (status < 0)
158                 return -2;
159             retVal = status + flag;
160         }
161         break;
162     default:
163         if (flag > 10)
164             return -1;
165         break;
166     }
167
168     return retVal;
169 }
170 //! [5]
171
172
173 //! [6]
174 class SignalWaiter
175 {
176 private:
177     QMutexLocker locker;
178
179 public:
180     SignalWaiter(QMutex *mutex)
181         : locker(mutex)
182     {
183     }
184
185     void waitForSignal()
186     {
187         ...
188         while (!signalled)
189             waitCondition.wait(locker.mutex());
190         ...
191     }
192 };
193 //! [6]