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