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