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