Fistman,
Him? Never listen to that guy. He’s a jerk.
However he does worry when folks assert that killing a thread
is safer than convincing it to exit of natural causes. Poor fellow
had a difficult childhood - they used to whip him with threads.
Well, whether or not this guy is a jerk, I have to agree on this
point: Killing a thread is really a last resort thing, if all else
fails.
If you’re worried about getting out of sync, ending up waiting for
ever, tell the thread to finish it’s work, and then wait with a
timeout. If it times out instead of finishing, kill it, and hope for
the best. (And don’t forget to do the cleaning up that the thread was
supposed to do…!)
[…]
Beats me. I did my own mixer for SDL and it’s crap too. One of the
reasons that it’s crap is that it queues up sounds by modifying a
pointer without any synchronisation (ha! storing a pointer
is “atomic”, what could go wrong!?), which of course won’t work on
the increasingly common non-uniprocessor machines.
You can’t even count on a 32 bit integer access being atomic in a 32
bit system, in some extreme cases…
Can I use the SDL thread synchronisation functions in the sound
callback?
Or is the sound callback not always a thread?
On Mac OS Classic and maybe some other platform, the callback runs in
some kind of interrupt context. The sync functions might still
work, though; not sure about that…
However, if you just want the simplest possible locking/unlocking, to
safely mess around with the sound engine data from the main thread,
you can use the SDL_LockAudio() and SDL_UnlockAudio() calls.
Another approach is to use a lock-free FIFO or similar construct to
pass commands from the main thread to the audio callback. This may be
a better idea if you send lots of “sound commands” from all over the
place. (Not sure if abusing the SDL_(Un)LockAudio() calls that way is
a good idea, though it’s probably not a problem for “normal use”.)
A hybrid approach would be to enqueue sound commands in a linked list
or other simple, non thread safe structure, and then execute them all
once per game logic cycle or something, with the audio callback
locked.
//David Olofson - Programmer, Composer, Open Source Advocate
.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --'On Friday 29 December 2006 23:05, Rhythmic Fistman wrote:
From: karx11erx