[SDL] When to call SDL_CondSignal()

Lazy Foo' lazyfoo at gmail.com
Sun Nov 12 20:51:54 PST 2006


I'm redoing the producer/consumer example in SDL.

Here are my produce() and consume() functions:

void produce()
{
    //Lock
    SDL_mutexP( bufferLock );
    
    //If the buffer is full
    if( buffer != NULL )
    {
        //Wait for buffer to be cleared
        SDL_CondWait( canProduce, bufferLock );
    }
    
    //Fill buffer
    buffer = images[ rand() % 5 ];
    
    //Signal consumer
    SDL_CondSignal( canConsume );
    
    //Unlock
    SDL_mutexV( bufferLock );
}

void consume()
{
    //Lock
    SDL_mutexP( bufferLock );
    
    //If the buffer is empty
    if( buffer == NULL )
    {
        //Wait for buffer to be filled
        SDL_CondWait( canConsume, bufferLock );
    }
    
    //Empty buffer
    buffer = NULL;

    //Signal producer
    SDL_CondSignal( canProduce );
    
    //Unlock
    SDL_mutexV( bufferLock );
}

I just want to make sure I'm supposed to call SDL_CondSignal() to signal the
other thread before or after calling SDL_mutexV() to unlock the mutex.

So am I doing this right?





More information about the SDL mailing list