0
|
1 |
<HTML
|
|
2 |
><HEAD
|
|
3 |
><TITLE
|
|
4 |
>Multi-threaded Programming</TITLE
|
|
5 |
><META
|
|
6 |
NAME="GENERATOR"
|
|
7 |
CONTENT="Modular DocBook HTML Stylesheet Version 1.61
|
|
8 |
"><LINK
|
|
9 |
REL="HOME"
|
|
10 |
TITLE="SDL Library Documentation"
|
|
11 |
HREF="index.html"><LINK
|
|
12 |
REL="UP"
|
|
13 |
TITLE="SDL Reference"
|
|
14 |
HREF="reference.html"><LINK
|
|
15 |
REL="PREVIOUS"
|
|
16 |
TITLE="SDL_CDtrack"
|
|
17 |
HREF="sdlcdtrack.html"><LINK
|
|
18 |
REL="NEXT"
|
|
19 |
TITLE="SDL_CreateThread"
|
|
20 |
HREF="sdlcreatethread.html"><META
|
|
21 |
NAME="KEYWORD"
|
|
22 |
CONTENT="threads"><META
|
|
23 |
NAME="KEYWORD"
|
|
24 |
CONTENT="function"></HEAD
|
|
25 |
><BODY
|
|
26 |
CLASS="CHAPTER"
|
|
27 |
BGCOLOR="#FFF8DC"
|
|
28 |
TEXT="#000000"
|
|
29 |
LINK="#0000ee"
|
|
30 |
VLINK="#551a8b"
|
|
31 |
ALINK="#ff0000"
|
|
32 |
><DIV
|
|
33 |
CLASS="NAVHEADER"
|
|
34 |
><TABLE
|
|
35 |
WIDTH="100%"
|
|
36 |
BORDER="0"
|
|
37 |
CELLPADDING="0"
|
|
38 |
CELLSPACING="0"
|
|
39 |
><TR
|
|
40 |
><TH
|
|
41 |
COLSPAN="3"
|
|
42 |
ALIGN="center"
|
|
43 |
>SDL Library Documentation</TH
|
|
44 |
></TR
|
|
45 |
><TR
|
|
46 |
><TD
|
|
47 |
WIDTH="10%"
|
|
48 |
ALIGN="left"
|
|
49 |
VALIGN="bottom"
|
|
50 |
><A
|
|
51 |
HREF="sdlcdtrack.html"
|
|
52 |
>Prev</A
|
|
53 |
></TD
|
|
54 |
><TD
|
|
55 |
WIDTH="80%"
|
|
56 |
ALIGN="center"
|
|
57 |
VALIGN="bottom"
|
|
58 |
></TD
|
|
59 |
><TD
|
|
60 |
WIDTH="10%"
|
|
61 |
ALIGN="right"
|
|
62 |
VALIGN="bottom"
|
|
63 |
><A
|
|
64 |
HREF="sdlcreatethread.html"
|
|
65 |
>Next</A
|
|
66 |
></TD
|
|
67 |
></TR
|
|
68 |
></TABLE
|
|
69 |
><HR
|
|
70 |
ALIGN="LEFT"
|
|
71 |
WIDTH="100%"></DIV
|
|
72 |
><DIV
|
|
73 |
CLASS="CHAPTER"
|
|
74 |
><H1
|
|
75 |
><A
|
|
76 |
NAME="THREAD"
|
|
77 |
>Chapter 12. Multi-threaded Programming</A
|
|
78 |
></H1
|
|
79 |
><DIV
|
|
80 |
CLASS="TOC"
|
|
81 |
><DL
|
|
82 |
><DT
|
|
83 |
><B
|
|
84 |
>Table of Contents</B
|
|
85 |
></DT
|
|
86 |
><DT
|
|
87 |
><A
|
|
88 |
HREF="sdlcreatethread.html"
|
|
89 |
>SDL_CreateThread</A
|
|
90 |
> — Creates a new thread of execution that shares its parent's properties.</DT
|
|
91 |
><DT
|
|
92 |
><A
|
|
93 |
HREF="sdlthreadid.html"
|
|
94 |
>SDL_ThreadID</A
|
|
95 |
> — Get the 32-bit thread identifier for the current thread.</DT
|
|
96 |
><DT
|
|
97 |
><A
|
|
98 |
HREF="sdlgetthreadid.html"
|
|
99 |
>SDL_GetThreadID</A
|
|
100 |
> — Get the SDL thread ID of a SDL_Thread</DT
|
|
101 |
><DT
|
|
102 |
><A
|
|
103 |
HREF="sdlwaitthread.html"
|
|
104 |
>SDL_WaitThread</A
|
|
105 |
> — Wait for a thread to finish.</DT
|
|
106 |
><DT
|
|
107 |
><A
|
|
108 |
HREF="sdlkillthread.html"
|
|
109 |
>SDL_KillThread</A
|
|
110 |
> — Gracelessly terminates the thread.</DT
|
|
111 |
><DT
|
|
112 |
><A
|
|
113 |
HREF="sdlcreatemutex.html"
|
|
114 |
>SDL_CreateMutex</A
|
|
115 |
> — Create a mutex</DT
|
|
116 |
><DT
|
|
117 |
><A
|
|
118 |
HREF="sdldestroymutex.html"
|
|
119 |
>SDL_DestroyMutex</A
|
|
120 |
> — Destroy a mutex</DT
|
|
121 |
><DT
|
|
122 |
><A
|
|
123 |
HREF="sdlmutexp.html"
|
|
124 |
>SDL_mutexP</A
|
|
125 |
> — Lock a mutex</DT
|
|
126 |
><DT
|
|
127 |
><A
|
|
128 |
HREF="sdlmutexv.html"
|
|
129 |
>SDL_mutexV</A
|
|
130 |
> — Unlock a mutex</DT
|
|
131 |
><DT
|
|
132 |
><A
|
|
133 |
HREF="sdlcreatesemaphore.html"
|
|
134 |
>SDL_CreateSemaphore</A
|
|
135 |
> — Creates a new semaphore and assigns an initial value to it.</DT
|
|
136 |
><DT
|
|
137 |
><A
|
|
138 |
HREF="sdldestroysemaphore.html"
|
|
139 |
>SDL_DestroySemaphore</A
|
|
140 |
> — Destroys a semaphore that was created by <A
|
|
141 |
HREF="sdlcreatesemaphore.html"
|
|
142 |
>SDL_CreateSemaphore</A
|
|
143 |
>.</DT
|
|
144 |
><DT
|
|
145 |
><A
|
|
146 |
HREF="sdlsemwait.html"
|
|
147 |
>SDL_SemWait</A
|
|
148 |
> — Lock a semaphore and suspend the thread if the semaphore value is zero.</DT
|
|
149 |
><DT
|
|
150 |
><A
|
|
151 |
HREF="sdlsemtrywait.html"
|
|
152 |
>SDL_SemTryWait</A
|
|
153 |
> — Attempt to lock a semaphore but don't suspend the thread.</DT
|
|
154 |
><DT
|
|
155 |
><A
|
|
156 |
HREF="sdlsemwaittimeout.html"
|
|
157 |
>SDL_SemWaitTimeout</A
|
|
158 |
> — Lock a semaphore, but only wait up to a specified maximum time.</DT
|
|
159 |
><DT
|
|
160 |
><A
|
|
161 |
HREF="sdlsempost.html"
|
|
162 |
>SDL_SemPost</A
|
|
163 |
> — Unlock a semaphore.</DT
|
|
164 |
><DT
|
|
165 |
><A
|
|
166 |
HREF="sdlsemvalue.html"
|
|
167 |
>SDL_SemValue</A
|
|
168 |
> — Return the current value of a semaphore.</DT
|
|
169 |
><DT
|
|
170 |
><A
|
|
171 |
HREF="sdlcreatecond.html"
|
|
172 |
>SDL_CreateCond</A
|
|
173 |
> — Create a condition variable</DT
|
|
174 |
><DT
|
|
175 |
><A
|
|
176 |
HREF="sdldestroycond.html"
|
|
177 |
>SDL_DestroyCond</A
|
|
178 |
> — Destroy a condition variable</DT
|
|
179 |
><DT
|
|
180 |
><A
|
|
181 |
HREF="sdlcondsignal.html"
|
|
182 |
>SDL_CondSignal</A
|
|
183 |
> — Restart a thread wait on a condition variable</DT
|
|
184 |
><DT
|
|
185 |
><A
|
|
186 |
HREF="sdlcondbroadcast.html"
|
|
187 |
>SDL_CondBroadcast</A
|
|
188 |
> — Restart all threads waiting on a condition variable</DT
|
|
189 |
><DT
|
|
190 |
><A
|
|
191 |
HREF="sdlcondwait.html"
|
|
192 |
>SDL_CondWait</A
|
|
193 |
> — Wait on a condition variable</DT
|
|
194 |
><DT
|
|
195 |
><A
|
|
196 |
HREF="sdlcondwaittimeout.html"
|
|
197 |
>SDL_CondWaitTimeout</A
|
|
198 |
> — Wait on a condition variable, with timeout</DT
|
|
199 |
></DL
|
|
200 |
></DIV
|
|
201 |
><P
|
|
202 |
>SDL provides functions for creating threads, mutexes, semphores and condition variables.</P
|
|
203 |
><P
|
|
204 |
>In general, you must be very aware of concurrency and data integrity issues
|
|
205 |
when writing multi-threaded programs. Some good guidelines include:
|
|
206 |
<P
|
|
207 |
></P
|
|
208 |
><UL
|
|
209 |
><LI
|
|
210 |
><P
|
|
211 |
>Don't call SDL video/event functions from separate threads</P
|
|
212 |
></LI
|
|
213 |
><LI
|
|
214 |
><P
|
|
215 |
>Don't use any library functions in separate threads</P
|
|
216 |
></LI
|
|
217 |
><LI
|
|
218 |
><P
|
|
219 |
>Don't perform any memory management in separate threads</P
|
|
220 |
></LI
|
|
221 |
><LI
|
|
222 |
><P
|
|
223 |
>Lock global variables which may be accessed by multiple threads</P
|
|
224 |
></LI
|
|
225 |
><LI
|
|
226 |
><P
|
|
227 |
>Never terminate threads, always set a flag and wait for them to quit</P
|
|
228 |
></LI
|
|
229 |
><LI
|
|
230 |
><P
|
|
231 |
>Think very carefully about all possible ways your code may interact</P
|
|
232 |
></LI
|
|
233 |
></UL
|
|
234 |
></P
|
|
235 |
><DIV
|
|
236 |
CLASS="NOTE"
|
|
237 |
><BLOCKQUOTE
|
|
238 |
CLASS="NOTE"
|
|
239 |
><P
|
|
240 |
><B
|
|
241 |
>Note: </B
|
|
242 |
>SDL's threading is not implemented on MacOS, due to that lack of preemptive thread support (eck!)</P
|
|
243 |
></BLOCKQUOTE
|
|
244 |
></DIV
|
|
245 |
></DIV
|
|
246 |
><DIV
|
|
247 |
CLASS="NAVFOOTER"
|
|
248 |
><HR
|
|
249 |
ALIGN="LEFT"
|
|
250 |
WIDTH="100%"><TABLE
|
|
251 |
WIDTH="100%"
|
|
252 |
BORDER="0"
|
|
253 |
CELLPADDING="0"
|
|
254 |
CELLSPACING="0"
|
|
255 |
><TR
|
|
256 |
><TD
|
|
257 |
WIDTH="33%"
|
|
258 |
ALIGN="left"
|
|
259 |
VALIGN="top"
|
|
260 |
><A
|
|
261 |
HREF="sdlcdtrack.html"
|
|
262 |
>Prev</A
|
|
263 |
></TD
|
|
264 |
><TD
|
|
265 |
WIDTH="34%"
|
|
266 |
ALIGN="center"
|
|
267 |
VALIGN="top"
|
|
268 |
><A
|
|
269 |
HREF="index.html"
|
|
270 |
>Home</A
|
|
271 |
></TD
|
|
272 |
><TD
|
|
273 |
WIDTH="33%"
|
|
274 |
ALIGN="right"
|
|
275 |
VALIGN="top"
|
|
276 |
><A
|
|
277 |
HREF="sdlcreatethread.html"
|
|
278 |
>Next</A
|
|
279 |
></TD
|
|
280 |
></TR
|
|
281 |
><TR
|
|
282 |
><TD
|
|
283 |
WIDTH="33%"
|
|
284 |
ALIGN="left"
|
|
285 |
VALIGN="top"
|
|
286 |
>SDL_CDtrack</TD
|
|
287 |
><TD
|
|
288 |
WIDTH="34%"
|
|
289 |
ALIGN="center"
|
|
290 |
VALIGN="top"
|
|
291 |
><A
|
|
292 |
HREF="reference.html"
|
|
293 |
>Up</A
|
|
294 |
></TD
|
|
295 |
><TD
|
|
296 |
WIDTH="33%"
|
|
297 |
ALIGN="right"
|
|
298 |
VALIGN="top"
|
|
299 |
>SDL_CreateThread</TD
|
|
300 |
></TR
|
|
301 |
></TABLE
|
|
302 |
></DIV
|
|
303 |
></BODY
|
|
304 |
></HTML
|
|
305 |
> |