Wiki Page Content

Revision 6 as of 2011-02-14 20:17:50

Clear message

DRAFT

SDL_CreateCond

Use this function to create a condition variable.

Syntax

SDL_cond* SDL_CreateCond(void)

Return Value

Returns the condition variable that is created.

Code Examples

*

SDL_cond *cond;

cond=SDL_CreateCond();
.
.
/* Do stuff */
.
.
SDL_DestroyCond(cond);

*
green

Remarks

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.

green


CategoryAPI, CategoryMutex

(Page Info.)
Feedback
Please include your contact information if you'd like to receive a reply.
Submit