I use a semaphore too, but only as a means of signalling back to the
’sending’ thread that the event has been read
My goal was basically to make WaitEvent block properly instead of ‘spin’. I
still like to do all my coding on the ‘main’ thread, so the timer callbacks
just push a user event and return very quickly. But this does mean
WaitEvent needs to be precise and not just delay(10) arbitrarily.
I got this going by posting to a semaphore in peepevents just after any
events are added, and doing a wait on this semaphore in WaitEvent instead
of the delay(10) when there are no events. The semaphore wait will often
return immediately here because I’m posting in peepevents more than I need
to, but that’s OK because it’ll eventually ‘flush’ the semaphore and block
for real. Not 100% nice, but better than just thrashing delay(0).
The real fix is probably to only post when an event as added to an empty
queue. The semaphore will still probably spin sometimes depending on how
events are being pulled from the queue, but it should be generally better.
I suppose the key question is whether SDL_SemWaitTimeout uses a more
efficient waiting mechanism
Good question. Looking at the code, the windows one does, but the linux one
only does if you have #defined HAVE_SEM_TIMEDWAIT - which I haven’t done
(yet)!
But the above fix doesn’t even need a timed semaphore now that I think
about it…On Mon, May 23, 2016 at 8:14 PM, rtrussell wrote:
marksibly wrote:
Actually, I sort of got this going using a semaphore
I use a semaphore too, but only as a means of signalling back to the
’sending’ thread that the event has been read (I need a functionality like
SendMessage in Windows).
I suppose the key question is whether SDL_SemWaitTimeout uses a more
efficient waiting mechanism (in terms of latency and CPU usage) than
SDL_WaitEventTimeout. If it does then I might be able to improve on my
existing approach, but in that case why wouldn’t SDL do the same itself?
Richard.
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org