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