1 \section{\class{wxThread
}}\label{wxthread
}
3 A thread is basically a path of execution through a program. Threads are
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 it 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.
11 There are two types of threads in wxWidgets:
{\it detached
} and
{\it joinable
}
12 ones, just as in the POSIX thread API (but unlike Win32 threads where all threads
13 are joinable). The difference between the two is that only joinable threads
14 can return a return code -- this is returned by the Wait() function. Detached
15 threads (the default type) cannot be waited for.
17 You shouldn't hurry to create all the threads joinable, however, because this
18 has a disadvantage as well: you
{\bf must
} Wait() for a joinable thread or the
19 system resources used by it will never be freed, and you also must delete the
20 corresponding wxThread object yourself. In contrast, detached threads are of the
21 "fire-and-forget" kind: you only have to start a detached thread and it will
22 terminate and destroy itself.
24 This means, of course, that all detached threads
{\bf must
} be created on the
25 heap because the thread will call
{\tt delete this;
} upon termination. Joinable
26 threads may be created on the stack although more usually they will be created
27 on the heap as well. Don't create global thread objects because they allocate
28 memory in their constructor, which will cause problems for the memory checking
29 system. Finally, another consequence of the handling of the above is that you
30 should never delete a detached thread yourself, as this will be done by the
31 thread itself when it terminates.
33 \wxheading{Derived from
}
37 \wxheading{Include files
}
43 \helpref{wxMutex
}{wxmutex
},
\helpref{wxCondition
}{wxcondition
},
\helpref{wxCriticalSection
}{wxcriticalsection
}
45 \latexignore{\rtfignore{\wxheading{Members
}}}
48 \membersection{wxThread::wxThread
}\label{wxthreadctor
}
50 \func{}{wxThread
}{\param{wxThreadKind
}{kind = wxTHREAD
\_DETACHED}}
52 This constructor creates a new detached (default) or joinable C++ thread object. It
53 does not create or start execution of the real thread -- for this you should
54 use the
\helpref{Create
}{wxthreadcreate
} and
\helpref{Run
}{wxthreadrun
} methods.
56 The possible values for
{\it kind
} parameters are:
59 \begin{twocollist
}\itemsep=
0pt
60 \twocolitem{{\bf wxTHREAD
\_DETACHED}}{Create a detached thread.
}
61 \twocolitem{{\bf wxTHREAD
\_JOINABLE}}{Create a joinable thread
}
65 \membersection{wxThread::
\destruct{wxThread
}}\label{wxthreaddtor
}
67 \func{}{\destruct{wxThread
}}{\void}
69 The destructor frees the resources associated with the thread. Notice that you
70 should never delete a detached thread -- you may only call
71 \helpref{Delete
}{wxthreaddelete
} on it or wait until it terminates (and auto
72 destructs) itself. Because the detached threads delete themselves, they can
73 only be allocated on the heap.
75 Joinable threads should be deleted explicitly. The
\helpref{Delete
}{wxthreaddelete
} and
\helpref{Kill
}{wxthreadkill
} functions
76 will not delete the C++ thread object. It is also safe to allocate them on
80 \membersection{wxThread::Create
}\label{wxthreadcreate
}
82 \func{wxThreadError
}{Create
}{\param{unsigned int
}{stackSize =
0}}
84 Creates a new thread. The thread object is created in the suspended state, and you
85 should call
\helpref{Run
}{wxthreadrun
} to start running it. You may optionally
86 specify the stack size to be allocated to it (Ignored on platforms that don't
87 support setting it explicitly, eg. Unix system without
88 \texttt{pthread
\_attr\_setstacksize}). If you do not specify the stack size,
89 the system's default value is used.
91 {\bf Warning:
} It is a good idea to explicitly specify a value as systems'
92 default values vary from just a couple of KB on some systems (BSD and
93 OS/
2 systems) to one or several MB (Windows, Solaris, Linux). So, if you
94 have a thread that requires more than just a few KB of memory, you will
95 have mysterious problems on some platforms but not on the common ones. On the
96 other hand, just indicating a large stack size by default will give you
97 performance issues on those systems with small default stack since those
98 typically use fully committed memory for the stack. On the contrary, if
99 use a lot of threads (say several hundred), virtual adress space can get tight
100 unless you explicitly specify a smaller amount of thread stack space for each
104 \wxheading{Return value
}
109 \begin{twocollist
}\itemsep=
0pt
110 \twocolitem{{\bf wxTHREAD
\_NO\_ERROR}}{There was no error.
}
111 \twocolitem{{\bf wxTHREAD
\_NO\_RESOURCE}}{There were insufficient resources to create a new thread.
}
112 \twocolitem{{\bf wxTHREAD
\_RUNNING}}{The thread is already running.
}
116 \membersection{wxThread::Delete
}\label{wxthreaddelete
}
118 \func{void
}{Delete
}{\void}
120 Calling
\helpref{Delete
}{wxthreaddelete
} is a graceful way to terminate the
121 thread. It asks the thread to terminate and, if the thread code is well
122 written, the thread will terminate after the next call to
123 \helpref{TestDestroy
}{wxthreadtestdestroy
} which should happen quite soon.
125 However, if the thread doesn't call
\helpref{TestDestroy
}{wxthreadtestdestroy
}
126 often enough (or at all), the function will not return immediately, but wait
127 until the thread terminates. As it may take a long time, and the message processing
128 is not stopped during this function execution, message handlers may be
129 called from inside it!
131 Delete() may be called for a thread in any state: running, paused or even not
132 yet created. Moreover, it must be called if
\helpref{Create
}{wxthreadcreate
} or
133 \helpref{Run
}{wxthreadrun
} fail in order to free the memory occupied by the
134 thread object. However, you should not call Delete() on a detached thread which
135 already terminated -- doing so will probably result in a crash because the
136 thread object doesn't exist any more.
138 For detached threads Delete() will also delete the C++ thread object, but it
139 will not do this for joinable ones.
141 This function can only be called from another thread context.
144 \membersection{wxThread::Entry
}\label{wxthreadentry
}
146 \func{virtual ExitCode
}{Entry
}{\void}
148 This is the entry point of the thread. This function is pure virtual and must
149 be implemented by any derived class. The thread execution will start here.
151 The returned value is the thread exit code which is only useful for
152 joinable threads and is the value returned by
\helpref{Wait
}{wxthreadwait
}.
154 This function is called by wxWidgets itself and should never be called
158 \membersection{wxThread::Exit
}\label{wxthreadexit
}
160 \func{void
}{Exit
}{\param{ExitCode
}{exitcode =
0}}
162 This is a protected function of the wxThread class and thus can only be called
163 from a derived class. It also can only be called in the context of this
164 thread, i.e. a thread can only exit from itself, not from another thread.
166 This function will terminate the OS thread (i.e. stop the associated path of
167 execution) and also delete the associated C++ object for detached threads.
168 \helpref{wxThread::OnExit
}{wxthreadonexit
} will be called just before exiting.
171 \membersection{wxThread::GetCPUCount
}\label{wxthreadgetcpucount
}
173 \func{static int
}{GetCPUCount
}{\void}
175 Returns the number of system CPUs or -
1 if the value is unknown.
179 \helpref{SetConcurrency
}{wxthreadsetconcurrency
}
182 \membersection{wxThread::GetCurrentId
}\label{wxthreadgetcurrentid
}
184 \func{static unsigned long
}{GetCurrentId
}{\void}
186 Returns the platform specific thread ID of the current thread as a
187 long. This can be used to uniquely identify threads, even if they are
191 \membersection{wxThread::GetId
}\label{wxthreadgetid
}
193 \constfunc{unsigned long
}{GetId
}{\void}
195 Gets the thread identifier: this is a platform dependent number that uniquely identifies the
196 thread throughout the system during its existence (i.e. the thread identifiers may be reused).
199 \membersection{wxThread::GetPriority
}\label{wxthreadgetpriority
}
201 \constfunc{int
}{GetPriority
}{\void}
203 Gets the priority of the thread, between zero and
100.
205 The following priorities are defined:
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}
215 \membersection{wxThread::IsAlive
}\label{wxthreadisalive
}
217 \constfunc{bool
}{IsAlive
}{\void}
219 Returns
\true if the thread is alive (i.e. started and not terminating).
221 Note that this function can only safely be used with joinable threads, not
222 detached ones as the latter delete themselves and so when the real thread is
223 no longer alive, it is not possible to call this function because
224 the wxThread object no longer exists.
226 \membersection{wxThread::IsDetached
}\label{wxthreadisdetached
}
228 \constfunc{bool
}{IsDetached
}{\void}
230 Returns
\true if the thread is of the detached kind,
\false if it is a joinable
234 \membersection{wxThread::IsMain
}\label{wxthreadismain
}
236 \func{static bool
}{IsMain
}{\void}
238 Returns
\true if the calling thread is the main application thread.
241 \membersection{wxThread::IsPaused
}\label{wxthreadispaused
}
243 \constfunc{bool
}{IsPaused
}{\void}
245 Returns
\true if the thread is paused.
248 \membersection{wxThread::IsRunning
}\label{wxthreadisrunning
}
250 \constfunc{bool
}{IsRunning
}{\void}
252 Returns
\true if the thread is running.
254 This method may only be safely used for joinable threads, see the remark in
255 \helpref{IsAlive
}{wxthreadisalive
}.
258 \membersection{wxThread::Kill
}\label{wxthreadkill
}
260 \func{wxThreadError
}{Kill
}{\void}
262 Immediately terminates the target thread.
{\bf This function is dangerous and should
263 be used with extreme care (and not used at all whenever possible)!
} The resources
264 allocated to the thread will not be freed and the state of the C runtime library
265 may become inconsistent. Use
\helpref{Delete()
}{wxthreaddelete
} instead.
267 For detached threads Kill() will also delete the associated C++ object.
268 However this will not happen for joinable threads and this means that you will
269 still have to delete the wxThread object yourself to avoid memory leaks.
270 In neither case
\helpref{OnExit
}{wxthreadonexit
} of the dying thread will be
271 called, so no thread-specific cleanup will be performed.
273 This function can only be called from another thread context, i.e. a thread
276 It is also an error to call this function for a thread which is not running or
277 paused (in the latter case, the thread will be resumed first) -- if you do it,
278 a
{\tt wxTHREAD
\_NOT\_RUNNING} error will be returned.
281 \membersection{wxThread::OnExit
}\label{wxthreadonexit
}
283 \func{void
}{OnExit
}{\void}
285 Called when the thread exits. This function is called in the context of the
286 thread associated with the wxThread object, not in the context of the main
287 thread. This function will not be called if the thread was
288 \helpref{killed
}{wxthreadkill
}.
290 This function should never be called directly.
293 \membersection{wxThread::Pause
}\label{wxthreadpause
}
295 \func{wxThreadError
}{Pause
}{\void}
297 Suspends the thread. Under some implementations (Win32), the thread is
298 suspended immediately, under others it will only be suspended when it calls
299 \helpref{TestDestroy
}{wxthreadtestdestroy
} for the next time (hence, if the
300 thread doesn't call it at all, it won't be suspended).
302 This function can only be called from another thread context.
305 \membersection{wxThread::Run
}\label{wxthreadrun
}
307 \func{wxThreadError
}{Run
}{\void}
309 Starts the thread execution. Should be called after
310 \helpref{Create
}{wxthreadcreate
}.
312 This function can only be called from another thread context.
315 \membersection{wxThread::SetPriority
}\label{wxthreadsetpriority
}
317 \func{void
}{SetPriority
}{\param{int
}{ priority
}}
319 Sets the priority of the thread, between $
0$ and $
100$. It can only be set
320 after calling
\helpref{Create()
}{wxthreadcreate
} but before calling
321 \helpref{Run()
}{wxthreadrun
}.
323 The following priorities are already defined:
326 \begin{twocollist
}\itemsep=
0pt
327 \twocolitem{{\bf WXTHREAD
\_MIN\_PRIORITY}}{0}
328 \twocolitem{{\bf WXTHREAD
\_DEFAULT\_PRIORITY}}{50}
329 \twocolitem{{\bf WXTHREAD
\_MAX\_PRIORITY}}{100}
333 \membersection{wxThread::Sleep
}\label{wxthreadsleep
}
335 \func{static void
}{Sleep
}{\param{unsigned long
}{milliseconds
}}
337 Pauses the thread execution for the given amount of time.
339 This function should be used instead of
\helpref{wxSleep
}{wxsleep
} by all worker
340 threads (i.e. all except the main one).
343 \membersection{wxThread::Resume
}\label{wxthreadresume
}
345 \func{wxThreadError
}{Resume
}{\void}
347 Resumes a thread suspended by the call to
\helpref{Pause
}{wxthreadpause
}.
349 This function can only be called from another thread context.
352 \membersection{wxThread::SetConcurrency
}\label{wxthreadsetconcurrency
}
354 \func{static bool
}{SetConcurrency
}{\param{size
\_t }{level
}}
356 Sets the thread concurrency level for this process. This is, roughly, the
357 number of threads that the system tries to schedule to run in parallel.
358 The value of $
0$ for
{\it level
} may be used to set the default one.
360 Returns
\true on success or false otherwise (for example, if this function is
361 not implemented for this platform -- currently everything except Solaris).
364 \membersection{wxThread::TestDestroy
}\label{wxthreadtestdestroy
}
366 \func{virtual bool
}{TestDestroy
}{\void}
368 This function should be called periodically by the thread to ensure that calls
369 to
\helpref{Pause
}{wxthreadpause
} and
\helpref{Delete
}{wxthreaddelete
} will
370 work. If it returns
\true, the thread should exit as soon as possible.
372 Notice that under some platforms (POSIX), implementation of
373 \helpref{Pause
}{wxthreadpause
} also relies on this function being called, so
374 not calling it would prevent both stopping and suspending thread from working.
377 \membersection{wxThread::This
}\label{wxthreadthis
}
379 \func{static wxThread *
}{This
}{\void}
381 Return the thread object for the calling thread. NULL is returned if the calling thread
382 is the main (GUI) thread, but
\helpref{IsMain
}{wxthreadismain
} should be used to test
383 whether the thread is really the main one because NULL may also be returned for the thread
384 not created with wxThread class. Generally speaking, the return value for such a thread
388 \membersection{wxThread::Yield
}\label{wxthreadyield
}
390 \func{void
}{Yield
}{\void}
392 Give the rest of the thread time slice to the system allowing the other threads to run.
393 See also
\helpref{Sleep()
}{wxthreadsleep
}.
396 \membersection{wxThread::Wait
}\label{wxthreadwait
}
398 \constfunc{ExitCode
}{Wait
}{\void}
400 Waits until the thread terminates and returns its exit code or
{\tt (ExitCode)-
1} on error.
402 You can only Wait() for joinable (not detached) threads.
404 This function can only be called from another thread context.