]>
Commit | Line | Data |
---|---|---|
eaaa6a06 JS |
1 | \section{\class{wxThread}}\label{wxthread} |
2 | ||
6e6110ee | 3 | A thread is basically a path of execution through a program. Threads are also |
631f1bfe | 4 | sometimes called {\it light-weight processes}, but the fundamental difference |
6e6110ee VZ |
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 | |
631f1bfe | 8 | easier to shoot oneself in the foot, so careful use of synchronization objects |
28d9589a | 9 | such as \helpref{mutexes}{wxmutex} and/or \helpref{critical sections}{wxcriticalsection} is recommended. |
eaaa6a06 JS |
10 | |
11 | \wxheading{Derived from} | |
12 | ||
13 | None. | |
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 |
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. | |
eaaa6a06 JS |
32 | |
33 | \membersection{wxThread::\destruct{wxThread}} | |
34 | ||
35 | \func{}{\destruct{wxThread}}{\void} | |
36 | ||
9063ea8e VZ |
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. | |
eaaa6a06 JS |
47 | |
48 | \membersection{wxThread::Create}\label{wxthreadcreate} | |
49 | ||
50 | \func{wxThreadError}{Create}{\void} | |
51 | ||
28d9589a VZ |
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. | |
eaaa6a06 JS |
54 | |
55 | \wxheading{Return value} | |
56 | ||
57 | One 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 |
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. | |
eaaa6a06 | 89 | |
9063ea8e | 90 | This 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 | |
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 | ||
9063ea8e VZ |
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}. | |
43191e0c | 101 | |
9063ea8e VZ |
102 | This function is called by wxWindows itself and should never be called |
103 | directly. | |
eaaa6a06 | 104 | |
9063ea8e VZ |
105 | \membersection{wxThread::GetId}\label{wxthreadgetid} |
106 | ||
107 | \constfunc{unsigned long}{GetId}{\void} | |
eaaa6a06 | 108 | |
28d9589a VZ |
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). | |
eaaa6a06 JS |
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 | ||
9063ea8e | 118 | The 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 | 131 | Returns 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 | ||
137 | Returns 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 | 143 | Returns 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 | ||
149 | Returns 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 |
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. | |
eaaa6a06 | 165 | |
9063ea8e VZ |
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 | ||
eaaa6a06 JS |
170 | \membersection{wxThread::OnExit}\label{wxthreadonexit} |
171 | ||
172 | \func{void}{OnExit}{\void} | |
173 | ||
28d9589a VZ |
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. | |
eaaa6a06 | 176 | |
9063ea8e VZ |
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 | ||
e2a6f233 JS |
190 | \membersection{wxThread::Run}\label{wxthreadrun} |
191 | ||
192 | \func{wxThreadError}{Run}{\void} | |
193 | ||
9063ea8e VZ |
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. | |
e2a6f233 | 198 | |
eaaa6a06 JS |
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 | |
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 | |
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 | ||
9063ea8e VZ |
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 | ||
28d9589a VZ |
239 | \membersection{wxThread::This}\label{wxthreadthis} |
240 | ||
9063ea8e | 241 | \func{static wxThread *}{This}{\void} |
28d9589a VZ |
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 | ||
9063ea8e | 251 | \func{void}{Yield}{\void} |
28d9589a VZ |
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}. | |
e2a6f233 | 255 | |
9063ea8e VZ |
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. |