[SDL] Is there a bug in Sdl ???

Sam Lantinga slouken at devolution.com
Sun Jan 19 20:24:02 PST 2003


> Vous ne voyez pas encore le problème ???
> # You don't see the problem ???

> Il y a un problème de famine !
> # there is a famine probleme !

> 1->(demande) : OK
> 1->(acquisition) : OK
> 2->(demande) : OK
> 1->(liberation) : OK
> 1->(demande) : OK
> 1->(acquisition) : Attention/Warning !!!

> Ce doit être au 2ème thread d'exécution la section critique car il l'a
> demander en premier, sinon ca risque d'être toujours le 1er qui rentre
> en section critique
> # it should be to the 2nd thread to go in critical section because it
> has asked in first : otherwise, the 1st thread could always go in
> critical section

You actually have a race condition - it's possible for both threads to
print "(demande)" and then either to actually lock the mutex.  In fact,
it's possible for this sequence to occur:

thread 1: print (demande)
thread 1: mutex lock - success
thread 1: print (acquisition)
-- context switch --
thread 2: print (demande)
-- context switch --
thread 1: print (liberation)
thread 1: print (demande)
thread 1: mutex lock - success
thread 1: print (acquisition)
-- context switch --
thread 2: mutex lock - block
-- context switch --
thread 1: print (liberation)
thread 1: print (demande)
thread 1: mutex lock - block
-- context switch --
thread 2: print (liberation)
...

What you really want to do to prevent thread starvation is guarantee that
a context switch will occur between the time you unlock the mutex and the
time that you try to re-lock the mutex.

For the record, I see identical behavior between the two programs here.

See ya,
	-Sam Lantinga, Software Engineer, Blizzard Entertainment




More information about the SDL mailing list