Wiki Page Content

Differences between revisions 5 and 6
Revision 5 as of 2010-10-17 19:04:28
Size: 936
Editor: SheenaSmith
Comment: update content - pointers, structs
Revision 6 as of 2011-02-14 20:17:50
Size: 1571
Editor: SheenaSmith
Comment: update content - 2/14 changeset 5295
Deletions are marked like this. Additions are marked like this.
Line 35: Line 35:
''You can add useful comments here'' 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.

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

None: SDL_CreateCond (last edited 2014-01-11 13:50:10 by PhilippWiesemann)

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