Commit 2fd57ffe authored by Sam Lantinga's avatar Sam Lantinga

Improved condition variable documentation

parent 86e36cce
...@@ -164,6 +164,31 @@ typedef struct SDL_cond SDL_cond; ...@@ -164,6 +164,31 @@ typedef struct SDL_cond SDL_cond;
/** /**
* Create a condition variable. * Create a condition variable.
*
* Typical use of condition variables:
*
* Thread A:
* SDL_LockMutex(lock);
* while ( ! condition ) {
* SDL_CondWait(cond, lock);
* }
* SDL_UnlockMutex(lock);
*
* Thread B:
* SDL_LockMutex(lock);
* ...
* condition = true;
* ...
* SDL_CondSignal(cond);
* SDL_UnlockMutex(lock);
*
* There is some discussion whether to signal the condition variable
* with the mutex locked or not. There is some potential performance
* benefit to unlocking first on some platforms, but there are some
* potential race conditions depending on how your code is structured.
*
* In general it's safer to signal the condition variable while the
* mutex is locked.
*/ */
extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void); extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
...@@ -181,6 +206,7 @@ extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond); ...@@ -181,6 +206,7 @@ extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
/** /**
* Restart all threads that are waiting on the condition variable. * Restart all threads that are waiting on the condition variable.
*
* \return 0 or -1 on error. * \return 0 or -1 on error.
*/ */
extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond); extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
......
...@@ -147,7 +147,7 @@ Typical use: ...@@ -147,7 +147,7 @@ Typical use:
Thread A: Thread A:
SDL_LockMutex(lock); SDL_LockMutex(lock);
while ( ! condition ) { while ( ! condition ) {
SDL_CondWait(cond); SDL_CondWait(cond, lock);
} }
SDL_UnlockMutex(lock); SDL_UnlockMutex(lock);
...@@ -156,6 +156,7 @@ Thread B: ...@@ -156,6 +156,7 @@ Thread B:
... ...
condition = true; condition = true;
... ...
SDL_CondSignal(cond);
SDL_UnlockMutex(lock); SDL_UnlockMutex(lock);
*/ */
int int
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment