I’m working on an application that has two threads that are largely independent with the exception of a small amount of shared state.
One thread W writes the shared state periodically and the other thread R reads a snapshot of the shared state periodically.
The strategy I’m using is as follows:
- Struct S visible to threads W and R holds shared state
- SDL_mutex to guard access to shared state
- Thread W uses SDL_LockMutex to lock mutex before updating struct S
- Thread W uses SDL_UnlockMutex to unlock mutex after updating struct S
- Thread R uses SDL_LockMutex to lock mutex before making a copy of struct S
- Thread R uses SDL_UnlockMutex to unlock mutex after making a copy of struct S
It’s a simple arrangement but I want to confirm it’s a sound approach that avoids data races.