]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/mutex.tex
Added periods
[wxWidgets.git] / docs / latex / wx / mutex.tex
CommitLineData
eaaa6a06
JS
1\section{\class{wxMutex}}\label{wxmutex}
2
6e6110ee
VZ
3A mutex object is a synchronization object whose state is set to signaled when
4it is not owned by any thread, and nonsignaled when it is owned. Its name comes
5from its usefulness in coordinating mutually-exclusive access to a shared
dc65f4ff
VZ
6resource as only one thread at a time can own a mutex object.
7
8Mutexes may be recursive in the sense that a thread can lock a mutex which it
9had already locked before (instead of dead locking the entire process in this
10situation by starting to wait on a mutex which will never be released while the
11thread is waiting) but using them is not recommended and they are {\bf not}
12recursive by default. The reason for this is that recursive mutexes are not
13supported by all Unix flavours and, worse, they cannot be used with
14\helpref{wxCondition}{wxcondition}.
6e6110ee 15
90e572f1
MR
16For example, when several threads use the data stored in the linked list,
17modifications to the list should only be allowed to one thread at a time
6e6110ee
VZ
18because during a new node addition the list integrity is temporarily broken
19(this is also called {\it program invariant}).
20
21\wxheading{Example}
22
23{\small%
24\begin{verbatim}
25 // this variable has an "s_" prefix because it is static: seeing an "s_" in
26 // a multithreaded program is in general a good sign that you should use a
27 // mutex (or a critical section)
28 static wxMutex *s_mutexProtectingTheGlobalData;
29
30 // we store some numbers in this global array which is presumably used by
31 // several threads simultaneously
32 wxArrayInt s_data;
33
34 void MyThread::AddNewNode(int num)
35 {
36 // ensure that no other thread accesses the list
37 s_mutexProtectingTheGlobalList->Lock();
38
39 s_data.Add(num);
40
41 s_mutexProtectingTheGlobalList->Unlock();
42 }
43
fab86f26 44 // return true if the given number is greater than all array elements
6e6110ee
VZ
45 bool MyThread::IsGreater(int num)
46 {
47 // before using the list we must acquire the mutex
48 wxMutexLocker lock(s_mutexProtectingTheGlobalData);
49
50 size_t count = s_data.Count();
51 for ( size_t n = 0; n < count; n++ )
52 {
53 if ( s_data[n] > num )
cc81d32f 54 return false;
6e6110ee
VZ
55 }
56
cc81d32f 57 return true;
6e6110ee
VZ
58 }
59\end{verbatim}
60}
61
62Notice how wxMutexLocker was used in the second function to ensure that the
cc81d32f 63mutex is unlocked in any case: whether the function returns true or false
6e6110ee
VZ
64(because the destructor of the local object {\it lock} is always called). Using
65this class instead of directly using wxMutex is, in general safer and is even
7a56de34 66more so if your program uses C++ exceptions.
eaaa6a06 67
dc65f4ff
VZ
68\wxheading{Constants}
69
70\begin{verbatim}
71enum wxMutexType
72{
73 // normal mutex: try to always use this one
74 wxMUTEX_DEFAULT,
75
76 // recursive mutex: don't use these ones with wxCondition
77 wxMUTEX_RECURSIVE
78};
79\end{verbatim}
80
eaaa6a06
JS
81\wxheading{Derived from}
82
83None.
84
954b8ae6
JS
85\wxheading{Include files}
86
87<wx/thread.h>
88
a7af285d
VZ
89\wxheading{Library}
90
91\helpref{wxBase}{librarieslist}
92
eaaa6a06
JS
93\wxheading{See also}
94
fa482912 95\helpref{wxThread}{wxthread}, \helpref{wxCondition}{wxcondition},
6e6110ee 96\helpref{wxMutexLocker}{wxmutexlocker}, \helpref{wxCriticalSection}{wxcriticalsection}
eaaa6a06
JS
97
98\latexignore{\rtfignore{\wxheading{Members}}}
99
696d13ee 100
3e79fa75 101\membersection{wxMutex::wxMutex}\label{wxmutexctor}
eaaa6a06 102
dc65f4ff 103\func{}{wxMutex}{\param{wxMutexType }{type = {\tt wxMUTEX\_DEFAULT}}}
eaaa6a06
JS
104
105Default constructor.
106
696d13ee 107
3e79fa75 108\membersection{wxMutex::\destruct{wxMutex}}\label{wxmutexdtor}
eaaa6a06
JS
109
110\func{}{\destruct{wxMutex}}{\void}
111
112Destroys the wxMutex object.
113
696d13ee 114
eaaa6a06
JS
115\membersection{wxMutex::Lock}\label{wxmutexlock}
116
117\func{wxMutexError}{Lock}{\void}
118
696d13ee
VZ
119Locks the mutex object. This is equivalent to
120\helpref{LockTimeout}{wxmutexlocktimeout} with infinite timeout.
eaaa6a06
JS
121
122\wxheading{Return value}
123
124One of:
125
126\twocolwidtha{7cm}
127\begin{twocollist}\itemsep=0pt
6e6110ee
VZ
128\twocolitem{{\bf wxMUTEX\_NO\_ERROR}}{There was no error.}
129\twocolitem{{\bf wxMUTEX\_DEAD\_LOCK}}{A deadlock situation was detected.}
eaaa6a06
JS
130\end{twocollist}
131
696d13ee
VZ
132
133\membersection{wxMutex::LockTimeout}\label{wxmutexlocktimeout}
134
135\func{wxMutexError}{LockTimeout}{\param{unsigned long}{ msec}}
136
137Try to lock the mutex object during the specified time interval.
138
139\wxheading{Return value}
140
141One of:
142
143\twocolwidtha{7cm}
144\begin{twocollist}\itemsep=0pt
145\twocolitem{{\bf wxMUTEX\_NO\_ERROR}}{Mutex successfully locked.}
146\twocolitem{{\bf wxMUTEX\_TIMEOUT}}{Mutex couldn't be acquired before timeout expiration.}
147\twocolitem{{\bf wxMUTEX\_DEAD\_LOCK}}{A deadlock situation was detected.}
148\end{twocollist}
149
150
eaaa6a06
JS
151\membersection{wxMutex::TryLock}\label{wxmutextrylock}
152
153\func{wxMutexError}{TryLock}{\void}
154
155Tries to lock the mutex object. If it can't, returns immediately with an error.
156
157\wxheading{Return value}
158
159One of:
160
161\twocolwidtha{7cm}
162\begin{twocollist}\itemsep=0pt
6e6110ee 163\twocolitem{{\bf wxMUTEX\_NO\_ERROR}}{There was no error.}
6e6110ee 164\twocolitem{{\bf wxMUTEX\_BUSY}}{The mutex is already locked by another thread.}
eaaa6a06
JS
165\end{twocollist}
166
696d13ee 167
eaaa6a06
JS
168\membersection{wxMutex::Unlock}\label{wxmutexunlock}
169
170\func{wxMutexError}{Unlock}{\void}
171
172Unlocks the mutex object.
173
174\wxheading{Return value}
175
176One of:
177
178\twocolwidtha{7cm}
179\begin{twocollist}\itemsep=0pt
6e6110ee 180\twocolitem{{\bf wxMUTEX\_NO\_ERROR}}{There was no error.}
2e91c905 181\twocolitem{{\bf wxMUTEX\_UNLOCKED}}{The calling thread doesn't own the mutex.}
eaaa6a06
JS
182\end{twocollist}
183