[SDL] Mutexes failing!

Michael Rickert mrickert85 at yahoo.com
Fri Nov 14 21:20:01 PST 2003


Although I notice a lack of mention in the doc project
of this, the mutex can be repeatedly locked... from
the same thread. Adding:

  for(n=0; n<11; n++)
  {
    if (SDL_mutexV(mutex)<0)
      fprintf(stderr,"Can't unlock mutex!?
%s\n",SDL_GetError());
    else
      fprintf(stderr,"Unlocked mutex.\n");
  }

right after the lock code gives this output:

Locked mutex.
Locked mutex.
Locked mutex.
Locked mutex.
Locked mutex.
Locked mutex.
Locked mutex.
Locked mutex.
Locked mutex.
Locked mutex.
Unlocked mutex.
Unlocked mutex.
Unlocked mutex.
Unlocked mutex.
Unlocked mutex.
Unlocked mutex.
Unlocked mutex.
Unlocked mutex.
Unlocked mutex.
Unlocked mutex.
Can't unlock mutex!? mutex not owned by this thread

(note: error on the 11th time).

This is so that you can call functions which
themselves lock the same mutex with safety in mind
without adverse effects. I believe this is called
're-entrant behavior'? This is a good thing. You don't
want your thread to hang because it locked a mutex
that only it can unlock...

--- Tyler Montbriand <tsm at accesscomm.ca> wrote:
> This might explain why I'm having such a hard time
> getting conditional 
> variables to work as I think they should...  this
> following code executes, 
> printing "Locked mutex." 10 times then exiting
> normally.  wtf?  What could 
> cause mutexes to fail so completely on a LINUX
> system?
> 
> #include <SDL/SDL.h>
> #include <SDL/SDL_mutex.h>
> #include <SDL/SDL_thread.h>
> 
> int main(int argc, char *argv[])
> {
>   int n;
>   SDL_mutex *mutex=NULL;
> 
>   if(SDL_Init(SDL_INIT_EVERYTHING)<0)
>   {
>     fprintf(stderr,"Can't init SDL:
> %s\n",SDL_GetError());
>     return(1);
>   }
> 
>   mutex=SDL_CreateMutex();
>   if(mutex==NULL)
>   {
>     fprintf(stderr,"Couldn't create mutex!\n");
>   {
>     fprintf(stderr,"Couldn't create mutex!\n");
>     return(1);
>   }
> 
>   for(n=0; n<10; n++)
>   {
>     if(SDL_mutexP(mutex)<0)
>       fprintf(stderr,"Can't lock mutex!?
> %s\n",SDL_GetError());
>     else
>       fprintf(stderr,"Locked mutex.\n");
>   }
> 
>   SDL_DestroyMutex(mutex);
> 
>   return(0);
> }
> 
> 
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl


__________________________________
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree




More information about the SDL mailing list