|
Size: 526
Comment: create page, add content
|
Size: 2490
Comment: add introductory information
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 5: | Line 5: |
| ||<tablewidth="100%" style="color: #FF0000;" :> DRAFT|| | |
| Line 10: | Line 11: |
| Other Includes: SDL_stdinc.h, SDL_error.h | Other Includes: SDL_thread.h, SDL_stdinc.h, SDL_error.h |
| Line 16: | Line 17: |
| Functions in this group provide thread synchronization primitives for functions in the [[CategoryThreads]] group. | Functions in this group provide thread synchronization primitives for functions in the [[CategoryThread|Thread Management]] group. === Mutex === ^If you have heard the phrase "mutually exclusive" then you already know the basic idea of a mutex. Mutex (__mut__ual __ex__clusion) functions are used to protect data structures. By coordinating multiple threads using or calling the same data source a mutex prevents interference between threads that may cause instability in the program. Threads take turns locking the mutex (accessing the resource) one at a time. Waiting threads are queued up based on the mutex algorithm and take their turn locking (accessing) the shared resource as each previous thread unlocks (releases) the mutex. Only the thread that has locked the mutex can access the data structure to read or write to it.^ ^''Analogy:'' This is a little like needing a key to access the restroom at a gas station.^ *^The mutex is the clerk at the counter who controls access to the key.^ *^The restroom is the data structure.^ *^An individual needing to use the restroom is a thread.^ ^The person who currently possesses the key has access to use the restroom and everyone else must wait. Once the key is returned to the clerk anyone waiting for the restroom may then attempt to get the key from the clerk. Eventually each waiting person will get their turn, but the restroom cannot be used by more than one person at a time in this way. (Please disregard the 'human factor' in this analogy, which allows individuals to break rules. Thankfully computers are much less prone to do so.)^ ;) === Semaphore === ^Semaphore functions are used to determine whether a data structure is available to be allocated to a thread. Semaphores count - up or down. In both cases the critical value is 0. For a semaphore counting up, 0 indicates that there is nothing available to be used/worked on. For a semaphore counting down, 0 indicates that^ === Condition Variable === |
DRAFT |
Thread Synchronization Primitives
Primary Include: SDL_mutex.h
Other Includes: SDL_thread.h, SDL_stdinc.h, SDL_error.h
Contents
Introduction
Functions in this group provide thread synchronization primitives for functions in the Thread Management group.
Mutex
If you have heard the phrase "mutually exclusive" then you already know the basic idea of a mutex. Mutex (__mut__ual __ex__clusion) functions are used to protect data structures. By coordinating multiple threads using or calling the same data source a mutex prevents interference between threads that may cause instability in the program. Threads take turns locking the mutex (accessing the resource) one at a time. Waiting threads are queued up based on the mutex algorithm and take their turn locking (accessing) the shared resource as each previous thread unlocks (releases) the mutex. Only the thread that has locked the mutex can access the data structure to read or write to it.
''Analogy:'' This is a little like needing a key to access the restroom at a gas station.
The mutex is the clerk at the counter who controls access to the key.
The restroom is the data structure.
An individual needing to use the restroom is a thread.
The person who currently possesses the key has access to use the restroom and everyone else must wait. Once the key is returned to the clerk anyone waiting for the restroom may then attempt to get the key from the clerk. Eventually each waiting person will get their turn, but the restroom cannot be used by more than one person at a time in this way. (Please disregard the 'human factor' in this analogy, which allows individuals to break rules. Thankfully computers are much less prone to do so.)
Semaphore
Semaphore functions are used to determine whether a data structure is available to be allocated to a thread. Semaphores count - up or down. In both cases the critical value is 0. For a semaphore counting up, 0 indicates that there is nothing available to be used/worked on. For a semaphore counting down, 0 indicates that
Condition Variable
