]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/thread.tex
* Stream doc update.
[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 calls {\it light-wight 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 a leg, 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{See also}
16
17 \helpref{wxMutex}{wxmutex}, \helpref{wxCondition}{wxcondition}, \helpref{wxCriticalSection}{wxarraywxcriticalsection}
18
19 \latexignore{\rtfignore{\wxheading{Members}}}
20
21 \membersection{wxThread::wxThread}\label{wxthreadctor}
22
23 \func{}{wxThread}{\void}
24
25 Default constructor: it doesn't create nor starts the thread.
26
27 \membersection{wxThread::\destruct{wxThread}}
28
29 \func{}{\destruct{wxThread}}{\void}
30
31 wxThread destructor is private, so you can not call it directly - i.e., deleting
32 wxThread objects is forbidden. Instead, you should use \helpref{Delete}{wxthreaddelete} or
33 \helpref{Kill}{wxthreadkill} methods. This also means that thread objects should
34 eb {\bf always} allocated on the heap (i.e. with {\it new}) because the functions
35 mentioned above will try to reclaim the storage from the heap.
36
37 \membersection{wxThread::Create}\label{wxthreadcreate}
38
39 \func{wxThreadError}{Create}{\void}
40
41 Creates a new thread. The thread object is created in the suspended state, you
42 should call \helpref{Run}{wxthreadrun} to start running it.
43
44 \wxheading{Return value}
45
46 One of:
47
48 \twocolwidtha{7cm}
49 \begin{twocollist}\itemsep=0pt
50 \twocolitem{{\bf wxTHREAD\_NO\_ERROR}}{There was no error.}
51 \twocolitem{{\bf wxTHREAD\_NO\_RESOURCE}}{There were insufficient resources to create a new thread.}
52 \twocolitem{{\bf wxTHREAD\_RUNNING}}{The thread is already running.}
53 \end{twocollist}
54
55 \membersection{wxThread::Delete}\label{wxthreaddelete}
56
57 \func{\void}{Delete}{\void}
58
59 This function should be called to terminate this thread. Unlike \helpref{Kill}{wxthreadkill}, it
60 gives the target thread the time to terminate gracefully. Because of this, however, this function
61 may not return immediately and if the thread is "hung" won't return at all. Also, message processing
62 is not stopped during this function execution, so the message handlers may be called from inside
63 it.
64
65 Delete() may be called for thread in any state: running, paused or even not yet created. Moreover,
66 it must be called if \helpref{Create}{wxthreadcreate} or \helpref{Run}{wxthreadrun} fail to free
67 the memory occupied by the thread object.
68
69 \membersection{wxThread::GetID}\label{wxthreadgetid}
70
71 \constfunc{unsigned long}{GetID}{\void}
72
73 Gets the thread identifier: this is a platform dependent number which uniquely identifies the
74 thread throughout the system during its existence (i.e. the thread identifiers may be reused).
75
76 \membersection{wxThread::GetPriority}\label{wxthreadgetpriority}
77
78 \constfunc{int}{GetPriority}{\void}
79
80 Gets the priority of the thread, between zero and 100.
81
82 The following priorities are already defined:
83
84 \twocolwidtha{7cm}
85 \begin{twocollist}\itemsep=0pt
86 \twocolitem{{\bf WXTHREAD\_MIN\_PRIORITY}}{0}
87 \twocolitem{{\bf WXTHREAD\_DEFAULT\_PRIORITY}}{50}
88 \twocolitem{{\bf WXTHREAD\_MAX\_PRIORITY}}{100}
89 \end{twocollist}
90
91 \membersection{wxThread::IsAlive}\label{wxthreadisalive}
92
93 \constfunc{bool}{IsAlive}{\void}
94
95 Returns TRUE if the thread is alive (i.e. started and not terminating).
96
97 \membersection{wxThread::IsMain}\label{wxthreadismain}
98
99 \constfunc{bool}{IsMain}{\void}
100
101 Returns TRUE if the calling thread is the main application thread.
102
103 \membersection{wxThread::IsPaused}\label{wxthreadispaused}
104
105 \constfunc{bool}{IsPaused}{\void}
106
107 Returns TRUE if the thread is paused.
108
109 \membersection{wxThread::IsRunning}\label{wxthreadisrunning}
110
111 \constfunc{bool}{IsRunning}{\void}
112
113 Returns TRUE if the thread is running.
114
115 \membersection{wxThread::Kill}\label{wxthreadkill}
116
117 \func{wxThreadError}{Kill}{\void}
118
119 Immediately terminates the target thread. {\bf This function is dangerous and should
120 be used with extreme care (and not used at all whenever possible)!} The resources
121 allocated to the thread will not be freed and the state of the C runtime library
122 may become inconsistent. Use \helpref{Delete()}{wxthreaddelete} instead.
123
124 \membersection{wxThread::OnExit}\label{wxthreadonexit}
125
126 \func{void}{OnExit}{\void}
127
128 Called when the thread exits. This function is called in the context of the thread
129 associated with the wxThread object, not in the context of the main thread.
130
131 \membersection{wxThread::SetPriority}\label{wxthreadsetpriority}
132
133 \func{void}{SetPriority}{\param{int}{ priority}}
134
135 Sets the priority of the thread, between zero and 100. This must be set before the thread is created.
136
137 The following priorities are already defined:
138
139 \twocolwidtha{7cm}
140 \begin{twocollist}\itemsep=0pt
141 \twocolitem{{\bf WXTHREAD\_MIN\_PRIORITY}}{0}
142 \twocolitem{{\bf WXTHREAD\_DEFAULT\_PRIORITY}}{50}
143 \twocolitem{{\bf WXTHREAD\_MAX\_PRIORITY}}{100}
144 \end{twocollist}
145
146 \membersection{wxThread::Sleep}\label{wxthreadsleep}
147
148 \func{\void}{Sleep}{\param{unsigned long }{milliseconds}}
149
150 Pauses the thread execution for the given amount of time.
151
152 This function should be used instead of \helpref{wxSleep}{wxsleep} by all worker
153 (i.e. all except the main one) threads.
154
155 \membersection{wxThread::This}\label{wxthreadthis}
156
157 \func{wxThread *}{This}{\void}
158
159 Return the thread object for the calling thread. NULL is returned if the calling thread
160 is the main (GUI) thread, but \helpref{IsMain}{wxthreadismain} should be used to test
161 whether the thread is really the main one because NULL may also be returned for the thread
162 not created with wxThread class. Generally speaking, the return value for such thread
163 is undefined.
164
165 \membersection{wxThread::Yield}\label{wxthreadyield}
166
167 \func{\void}{Yield}{\void}
168
169 Give the rest of the thread time slice to the system allowing the other threads to run.
170 See also \helpref{Sleep()}{wxthreadsleep}.