]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/thread.tex
SetTitle method (internal use only) changed to virtual OnSetTitle, added GetOpenedPag...
[wxWidgets.git] / docs / latex / wx / thread.tex
1 \section{\class{wxThread}}\label{wxthread}
2
3 A thread is basically a path of execution through a program. Threads are also
4 sometimes called {\it light-weight processes}, but the fundamental difference
5 between threads and processes is that memory spaces of different processes are
6 separated while all threads share the same address space. While it makes it
7 much easier to share common data between several threads, it also makes much
8 easier to shoot oneself in the foot, so careful use of synchronization objects
9 such as \helpref{mutexes}{wxmutex} and/or \helpref{critical sections}{wxcriticalsection} is recommended.
10
11 \wxheading{Derived from}
12
13 None.
14
15 \wxheading{Include files}
16
17 <wx/thread.h>
18
19 \wxheading{See also}
20
21 \helpref{wxMutex}{wxmutex}, \helpref{wxCondition}{wxcondition}, \helpref{wxCriticalSection}{wxcriticalsection}
22
23 \latexignore{\rtfignore{\wxheading{Members}}}
24
25 \membersection{wxThread::wxThread}\label{wxthreadctor}
26
27 \func{}{wxThread}{\void}
28
29 Constructor creates a new detached (default) or joinable C++ thread object. It
30 does not create (or starts execution of) the real thread - for this you should
31 use \helpref{Create}{wxthreadcreate} and \helpref{Run}{wxthreadrun} methods.
32
33 \membersection{wxThread::\destruct{wxThread}}
34
35 \func{}{\destruct{wxThread}}{\void}
36
37 Destructor frees the ressources associated with the thread. Notice that you
38 should never delete a detached thread - you may only call
39 \helpref{Delete}{wxthreaddelete} on it or wait until it terminates (and auto
40 destructs) itself. Because the detached threads delete themselves, they can
41 only be allocated on the heap.
42
43 The joinable threads, however, may and should be deleted explicitly and
44 \helpref{Delete}{wxthreaddelete} and \helpref{Kill}{wxthreadkill} functions
45 will not delete the C++ thread object. It is also safe to allocate them on
46 stack.
47
48 \membersection{wxThread::Create}\label{wxthreadcreate}
49
50 \func{wxThreadError}{Create}{\void}
51
52 Creates a new thread. The thread object is created in the suspended state, you
53 should call \helpref{Run}{wxthreadrun} to start running it.
54
55 \wxheading{Return value}
56
57 One of:
58
59 \twocolwidtha{7cm}
60 \begin{twocollist}\itemsep=0pt
61 \twocolitem{{\bf wxTHREAD\_NO\_ERROR}}{There was no error.}
62 \twocolitem{{\bf wxTHREAD\_NO\_RESOURCE}}{There were insufficient resources to create a new thread.}
63 \twocolitem{{\bf wxTHREAD\_RUNNING}}{The thread is already running.}
64 \end{twocollist}
65
66 \membersection{wxThread::Delete}\label{wxthreaddelete}
67
68 \func{void}{Delete}{\void}
69
70 Calling \helpref{Delete}{wxthreaddelete} is a graceful way to terminate the
71 thread. It asks the thread to terminate and, if the thread code is well
72 written, the thread will terminate after the next call to
73 \helpref{TestDestroy}{wxthreadtestdestroy} which should happen quiet soon.
74
75 However, if the thread doesn't call \helpref{TestDestroy}{wxthreadtestdestroy}
76 often enough (or at all), the function will not return immediately, but wait
77 until the thread terminates. As it may take a long time, the message processing
78 is not stopped during this function execution, so the message handlers may be
79 called from inside it!
80
81 Delete() may be called for thread in any state: running, paused or even not yet
82 created. Moreover, it must be called if \helpref{Create}{wxthreadcreate} or
83 \helpref{Run}{wxthreadrun} failed for a detached thread to free the memory
84 occupied by the thread object (it will be done in the destructor for joinable
85 threads).
86
87 For detached threads Delete() will also delete the C++ thread object, but it
88 will not do this for joinable ones.
89
90 This function can only be called from another thread context.
91
92 \membersection{wxThread::Entry}\label{wxthreadentry}
93
94 \func{virtual ExitCode}{Entry}{\void}
95
96 This is the entry point of the thread. This function is pure virtual and must
97 be implemented by any derived class. The thread execution will start here.
98
99 The returned value is the thread exit code which is only useful for the
100 joinable threads and is the value returned by \helpref{Wait}{wxthreadwait}.
101
102 This function is called by wxWindows itself and should never be called
103 directly.
104
105 \membersection{wxThread::GetId}\label{wxthreadgetid}
106
107 \constfunc{unsigned long}{GetId}{\void}
108
109 Gets the thread identifier: this is a platform dependent number which uniquely identifies the
110 thread throughout the system during its existence (i.e. the thread identifiers may be reused).
111
112 \membersection{wxThread::GetPriority}\label{wxthreadgetpriority}
113
114 \constfunc{int}{GetPriority}{\void}
115
116 Gets the priority of the thread, between zero and 100.
117
118 The following priorities are defined:
119
120 \twocolwidtha{7cm}
121 \begin{twocollist}\itemsep=0pt
122 \twocolitem{{\bf WXTHREAD\_MIN\_PRIORITY}}{0}
123 \twocolitem{{\bf WXTHREAD\_DEFAULT\_PRIORITY}}{50}
124 \twocolitem{{\bf WXTHREAD\_MAX\_PRIORITY}}{100}
125 \end{twocollist}
126
127 \membersection{wxThread::IsAlive}\label{wxthreadisalive}
128
129 \constfunc{bool}{IsAlive}{\void}
130
131 Returns TRUE if the thread is alive (i.e. started and not terminating).
132
133 \membersection{wxThread::IsDetached}\label{wxthreadisdetached}
134
135 \constfunc{bool}{IsDetached}{\void}
136
137 Returns TRUE if the thread is of detached kind, FALSE if it is a joinable one.
138
139 \membersection{wxThread::IsMain}\label{wxthreadismain}
140
141 \func{static bool}{IsMain}{\void}
142
143 Returns TRUE if the calling thread is the main application thread.
144
145 \membersection{wxThread::IsPaused}\label{wxthreadispaused}
146
147 \constfunc{bool}{IsPaused}{\void}
148
149 Returns TRUE if the thread is paused.
150
151 \membersection{wxThread::IsRunning}\label{wxthreadisrunning}
152
153 \constfunc{bool}{IsRunning}{\void}
154
155 Returns TRUE if the thread is running.
156
157 \membersection{wxThread::Kill}\label{wxthreadkill}
158
159 \func{wxThreadError}{Kill}{\void}
160
161 Immediately terminates the target thread. {\bf This function is dangerous and should
162 be used with extreme care (and not used at all whenever possible)!} The resources
163 allocated to the thread will not be freed and the state of the C runtime library
164 may become inconsistent. Use \helpref{Delete()}{wxthreaddelete} instead.
165
166 For detached threads Kill() will also delete the associated C++ object.
167
168 This function can only be called from another thread context.
169
170 \membersection{wxThread::OnExit}\label{wxthreadonexit}
171
172 \func{void}{OnExit}{\void}
173
174 Called when the thread exits. This function is called in the context of the thread
175 associated with the wxThread object, not in the context of the main thread.
176
177 This function should never be called directly.
178
179 \membersection{wxThread::Pause}\label{wxthreadpause}
180
181 \func{wxThreadError}{Pause}{\void}
182
183 Suspends the thread. Under some implementations (Win32), the thread is
184 suspended immediately, under others it will only be suspended when it calls
185 \helpref{TestDestroy}{wxthreadtestdestroy} for the next time (hence, if the
186 thread doesn't call it at all, it won't be suspended).
187
188 This function can only be called from another thread context.
189
190 \membersection{wxThread::Run}\label{wxthreadrun}
191
192 \func{wxThreadError}{Run}{\void}
193
194 Starts the thread execution. Should be called after
195 \helpref{Create}{wxthreadcreate}.
196
197 This function can only be called from another thread context.
198
199 \membersection{wxThread::SetPriority}\label{wxthreadsetpriority}
200
201 \func{void}{SetPriority}{\param{int}{ priority}}
202
203 Sets the priority of the thread, between zero and 100. This must be set before the thread is created.
204
205 The following priorities are already defined:
206
207 \twocolwidtha{7cm}
208 \begin{twocollist}\itemsep=0pt
209 \twocolitem{{\bf WXTHREAD\_MIN\_PRIORITY}}{0}
210 \twocolitem{{\bf WXTHREAD\_DEFAULT\_PRIORITY}}{50}
211 \twocolitem{{\bf WXTHREAD\_MAX\_PRIORITY}}{100}
212 \end{twocollist}
213
214 \membersection{wxThread::Sleep}\label{wxthreadsleep}
215
216 \func{static void}{Sleep}{\param{unsigned long }{milliseconds}}
217
218 Pauses the thread execution for the given amount of time.
219
220 This function should be used instead of \helpref{wxSleep}{wxsleep} by all worker
221 (i.e. all except the main one) threads.
222
223 \membersection{wxThread::Resume}\label{wxthreadresume}
224
225 \func{wxThreadError}{Resume}{\void}
226
227 Resumes a thread suspended by the call to \helpref{Pause}{wxthreadpause}.
228
229 This function can only be called from another thread context.
230
231 \membersection{wxThread::TestDestroy}\label{wxthreadtestdestroy}
232
233 \func{bool}{TestDestroy}{\void}
234
235 This function should be periodically called by the thread to ensure that calls
236 to \helpref{Pause}{wxthreadpause} and \helpref{Delete}{wxthreaddelete} will
237 work. If it returns TRUE, the thread should exit as soon as possible.
238
239 \membersection{wxThread::This}\label{wxthreadthis}
240
241 \func{static wxThread *}{This}{\void}
242
243 Return the thread object for the calling thread. NULL is returned if the calling thread
244 is the main (GUI) thread, but \helpref{IsMain}{wxthreadismain} should be used to test
245 whether the thread is really the main one because NULL may also be returned for the thread
246 not created with wxThread class. Generally speaking, the return value for such thread
247 is undefined.
248
249 \membersection{wxThread::Yield}\label{wxthreadyield}
250
251 \func{void}{Yield}{\void}
252
253 Give the rest of the thread time slice to the system allowing the other threads to run.
254 See also \helpref{Sleep()}{wxthreadsleep}.
255
256 \membersection{wxThread::Wait}\label{wxthreadwait}
257
258 \constfunc{ExitCode}{Wait}{\void}
259
260 Waits until the thread terminates and returns its exit code or {\tt
261 (ExitCode)-1} on error.
262
263 You can only Wait() for joinable (not detached) threads.
264
265 This function can only be called from another thread context.