# Waiting for events and callbacks

**URL:** <https://discourse.libsdl.org/t/waiting-for-events-and-callbacks/7707>\
**Category:** SDL Development\
**Created:** [November 7, 2002, 5:56pm UTC](https://discourse.libsdl.org/t/waiting-for-events-and-callbacks/7707 "2002-11-07T17:56:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Thielen](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/ce73a5/32.png) [@Christopher\_Thielen](https://discourse.libsdl.org/u/Christopher_Thielen)\
**Post date:** [November 7, 2002, 5:56pm UTC](https://discourse.libsdl.org/t/waiting-for-events-and-callbacks/7707/1 "2002-11-07T17:56:01Z")

</div>

Hello,  
I have a problem and I was wondering if anybody could offer  
suggestions. I have a simple user interface that uses SDL to draw and  
get events. The interface uses callbacks, including callbacks for  
timers, so say, every 35 ms, it’ll issue a callback. The problem is,  
with SDL\_PollEvent(), cpu is sucked up (and adding a SDL\_Delay(1) fixes  
that problem but makes input response far too sluggish) but with  
SDL\_WaitEvent(), an event has to occur before SDL\_WaitEvent() returns  
and my code checks for timer callbacks … so timers are thrown off.  
Any suggestions as to how I can keep cpu usage down, keep input  
response fast, and issue the callbacks when I need?–  
Chris Thielen \<@Christopher\_Thielen\>

---

<div class="post-metadata">

**Author:** ![Daniel\_Goertzen](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/ecd19e/32.png) [@Daniel\_Goertzen](https://discourse.libsdl.org/u/Daniel_Goertzen)\
**Post date:** [November 7, 2002, 7:43pm UTC](https://discourse.libsdl.org/t/waiting-for-events-and-callbacks/7707/2 "2002-11-07T19:43:01Z")

</div>

In your timer callback use SDL\_PushEvent() to unblock the SDL\_WaitEvent()  
call.

Dan.\> ----- Original Message -----

> From: [chris@luethy.net](mailto:chris@luethy.net) (Christopher Thielen)  
> To:   
> Sent: Thursday, November 07, 2002 7:47 PM  
> Subject: [SDL] waiting for events and callbacks
> 
> > Hello,  
> > I have a problem and I was wondering if anybody could offer  
> > suggestions. I have a simple user interface that uses SDL to draw and  
> > get events. The interface uses callbacks, including callbacks for  
> > timers, so say, every 35 ms, it’ll issue a callback. The problem is,  
> > with SDL\_PollEvent(), cpu is sucked up (and adding a SDL\_Delay(1) fixes  
> > that problem but makes input response far too sluggish) but with  
> > SDL\_WaitEvent(), an event has to occur before SDL\_WaitEvent() returns  
> > and my code checks for timer callbacks … so timers are thrown off.  
> > Any suggestions as to how I can keep cpu usage down, keep input  
> > response fast, and issue the callbacks when I need?
> > 
> > –  
> > Chris Thielen
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [libsdl.org](http://libsdl.org)  
> > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)

---

<div class="post-metadata">

**Author:** ![Christopher\_Thielen](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/ce73a5/32.png) [@Christopher\_Thielen](https://discourse.libsdl.org/u/Christopher_Thielen)\
**Post date:** [November 8, 2002, 7:19am UTC](https://discourse.libsdl.org/t/waiting-for-events-and-callbacks/7707/3 "2002-11-08T07:19:00Z")

</div>

If I use SDL\_PushEvent() while SDL\_WaitEvent() is called, then  
SDL\_WaitEvent() will return and produce the desired effect, however, how  
do I call SDL\_PushEvent() while SDL\_WaitEvent() is running? It’s not  
multithreaded, and the timer callback is only called after  
SDL\_WaitEvent() is returned, so that would only push the event after the  
program has halted for too long on SDL\_WaitEvent(). Should the program  
be multithreaded or am I misunderstanding what you’ve said?On Thu, 2002-11-07 at 19:41, Daniel Goertzen wrote:

> In your timer callback use SDL\_PushEvent() to unblock the SDL\_WaitEvent()  
> call.
> 
> Dan.
> 
> ----- Original Message -----  
> From: “Chris Thielen” \<@Christopher\_Thielen\>  
> To:   
> Sent: Thursday, November 07, 2002 7:47 PM  
> Subject: [SDL] waiting for events and callbacks
> 
> > Hello,  
> > I have a problem and I was wondering if anybody could offer  
> > suggestions. I have a simple user interface that uses SDL to draw and  
> > get events. The interface uses callbacks, including callbacks for  
> > timers, so say, every 35 ms, it’ll issue a callback. The problem is,  
> > with SDL\_PollEvent(), cpu is sucked up (and adding a SDL\_Delay(1) fixes  
> > that problem but makes input response far too sluggish) but with  
> > SDL\_WaitEvent(), an event has to occur before SDL\_WaitEvent() returns  
> > and my code checks for timer callbacks … so timers are thrown off.  
> > Any suggestions as to how I can keep cpu usage down, keep input  
> > response fast, and issue the callbacks when I need?
> > 
> > –  
> > Chris Thielen \<@Christopher\_Thielen\>
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [libsdl.org](http://libsdl.org)  
> > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
> 
> * * *
> 
> SDL mailing list  
> SDL at [libsdl.org](http://libsdl.org)  
> [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)  
> –  
> Chris Thielen \<@Christopher\_Thielen\>

---

<div class="post-metadata">

**Author:** ![Bob\_Pendleton](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@Bob\_Pendleton](https://discourse.libsdl.org/u/Bob_Pendleton)\
**Post date:** [November 8, 2002, 10:13am UTC](https://discourse.libsdl.org/t/waiting-for-events-and-callbacks/7707/4 "2002-11-08T10:13:01Z")

</div>

> If I use SDL\_PushEvent() while SDL\_WaitEvent() is called, then  
> SDL\_WaitEvent() will return and produce the desired effect, however, how  
> do I call SDL\_PushEvent() while SDL\_WaitEvent() is running? It’s not  
> multithreaded, and the timer callback is only called after  
> SDL\_WaitEvent() is returned, so that would only push the event after the  
> program has halted for too long on SDL\_WaitEvent(). Should the program  
> be multithreaded or am I misunderstanding what you’ve said?

Use a different timer to push the event.

```
	Bob PendletonOn Fri, 2002-11-08 at 09:10, Chris Thielen wrote:

```

> On Thu, 2002-11-07 at 19:41, Daniel Goertzen wrote:
> 
> > In your timer callback use SDL\_PushEvent() to unblock the SDL\_WaitEvent()  
> > call.
> > 
> > Dan.
> > 
> > ----- Original Message -----  
> > From: “Chris Thielen”   
> > To:   
> > Sent: Thursday, November 07, 2002 7:47 PM  
> > Subject: [SDL] waiting for events and callbacks
> > 
> > > Hello,  
> > > I have a problem and I was wondering if anybody could offer  
> > > suggestions. I have a simple user interface that uses SDL to draw and  
> > > get events. The interface uses callbacks, including callbacks for  
> > > timers, so say, every 35 ms, it’ll issue a callback. The problem is,  
> > > with SDL\_PollEvent(), cpu is sucked up (and adding a SDL\_Delay(1) fixes  
> > > that problem but makes input response far too sluggish) but with  
> > > SDL\_WaitEvent(), an event has to occur before SDL\_WaitEvent() returns  
> > > and my code checks for timer callbacks … so timers are thrown off.  
> > > Any suggestions as to how I can keep cpu usage down, keep input  
> > > response fast, and issue the callbacks when I need?
> > > 
> > > –  
> > > Chris Thielen
> > > 
> > > * * *
> > > 
> > > SDL mailing list  
> > > SDL at [libsdl.org](http://libsdl.org)  
> > > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [libsdl.org](http://libsdl.org)  
> > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)  
> > –  
> > Chris Thielen
> 
> * * *
> 
> SDL mailing list  
> SDL at [libsdl.org](http://libsdl.org)  
> [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)  
> –  
> ±-----------------------------------+

- Bob Pendleton, consultant for hire +
- Resume: [http://www.jump.net/~bobp](http://www.jump.net/~bobp) +
- Email: @Bob_Pendleton +  
±-----------------------------------+

---

<div class="post-metadata">

**Author:** ![Christopher\_Thielen](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/ce73a5/32.png) [@Christopher\_Thielen](https://discourse.libsdl.org/u/Christopher_Thielen)\
**Post date:** [November 8, 2002, 3:50pm UTC](https://discourse.libsdl.org/t/waiting-for-events-and-callbacks/7707/5 "2002-11-08T15:50:01Z")

</div>

> > If I use SDL\_PushEvent() while SDL\_WaitEvent() is called, then  
> > SDL\_WaitEvent() will return and produce the desired effect, however, how  
> > do I call SDL\_PushEvent() while SDL\_WaitEvent() is running? It’s not  
> > multithreaded, and the timer callback is only called after  
> > SDL\_WaitEvent() is returned, so that would only push the event after the  
> > program has halted for too long on SDL\_WaitEvent(). Should the program  
> > be multithreaded or am I misunderstanding what you’ve said?
> 
> Use a different timer to push the event.

it wouldn’t matter. the code literally goes:

SDL\_WaitEvent()  
Check for timer event, if so, callback (for all timers)  
repeat

the problem is waitevent() holds up the code and pollevent() is cpu  
intensive, whereas pollevent() with sdl\_delay() is too sluggish, even on  
a one millisecond delay.

so another timer would still have to wait for SDL\_WaitEvent(). is a  
multithreaded app the only way to do this?On Fri, 2002-11-08 at 10:19, Bob Pendleton wrote:

> On Fri, 2002-11-08 at 09:10, Chris Thielen wrote:

> > On Thu, 2002-11-07 at 19:41, Daniel Goertzen wrote:
> > 
> > > In your timer callback use SDL\_PushEvent() to unblock the SDL\_WaitEvent()  
> > > call.
> > > 
> > > Dan.
> > > 
> > > ----- Original Message -----  
> > > From: “Chris Thielen” \<@Christopher\_Thielen\>  
> > > To:   
> > > Sent: Thursday, November 07, 2002 7:47 PM  
> > > Subject: [SDL] waiting for events and callbacks
> > > 
> > > > Hello,  
> > > > I have a problem and I was wondering if anybody could offer  
> > > > suggestions. I have a simple user interface that uses SDL to draw and  
> > > > get events. The interface uses callbacks, including callbacks for  
> > > > timers, so say, every 35 ms, it’ll issue a callback. The problem is,  
> > > > with SDL\_PollEvent(), cpu is sucked up (and adding a SDL\_Delay(1) fixes  
> > > > that problem but makes input response far too sluggish) but with  
> > > > SDL\_WaitEvent(), an event has to occur before SDL\_WaitEvent() returns  
> > > > and my code checks for timer callbacks … so timers are thrown off.  
> > > > Any suggestions as to how I can keep cpu usage down, keep input  
> > > > response fast, and issue the callbacks when I need?
> > > > 
> > > > –  
> > > > Chris Thielen \<@Christopher\_Thielen\>
> > > > 
> > > > * * *
> > > > 
> > > > SDL mailing list  
> > > > SDL at [libsdl.org](http://libsdl.org)  
> > > > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
> > > 
> > > * * *
> > > 
> > > SDL mailing list  
> > > SDL at [libsdl.org](http://libsdl.org)  
> > > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)  
> > > –  
> > > Chris Thielen \<@Christopher\_Thielen\>
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [libsdl.org](http://libsdl.org)  
> > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)  
> > –  
> > Chris Thielen \<@Christopher\_Thielen\>

---

<div class="post-metadata">

**Author:** ![Bob\_Pendleton](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@Bob\_Pendleton](https://discourse.libsdl.org/u/Bob_Pendleton)\
**Post date:** [November 8, 2002, 6:49pm UTC](https://discourse.libsdl.org/t/waiting-for-events-and-callbacks/7707/6 "2002-11-08T18:49:01Z")

</div>

No, it does work. You send an event _FROM_ the timer callback. That  
event causes WaitEvent to return. The code for the callback looks like:

Uint32 timerCallback(Uint32 interval, void \*param)  
{  
SDL\_Event event;

event.type = SDL\_USEREVENT;  
event.user.code = MY\_TIMEREVENT;  
event.user.data1 = (void \*)SDL\_GetTicks();  
event.user.data2 = (void \*)0;

SDL\_PushEvent(&event);

return interval;  
}

The event is generated in the timer thread, not in the main thread. Think  
about it.

```
	Bob PendletonOn Fri, 2002-11-08 at 17:40, Chris Thielen wrote:

```

> On Fri, 2002-11-08 at 10:19, Bob Pendleton wrote:
> 
> > On Fri, 2002-11-08 at 09:10, Chris Thielen wrote:
> > 
> > > If I use SDL\_PushEvent() while SDL\_WaitEvent() is called, then  
> > > SDL\_WaitEvent() will return and produce the desired effect, however, how  
> > > do I call SDL\_PushEvent() while SDL\_WaitEvent() is running? It’s not  
> > > multithreaded, and the timer callback is only called after  
> > > SDL\_WaitEvent() is returned, so that would only push the event after the  
> > > program has halted for too long on SDL\_WaitEvent(). Should the program  
> > > be multithreaded or am I misunderstanding what you’ve said?
> > 
> > Use a different timer to push the event.
> 
> it wouldn’t matter. the code literally goes:
> 
> SDL\_WaitEvent()  
> Check for timer event, if so, callback (for all timers)  
> repeat

–  
±-----------------------------------+

- Bob Pendleton, consultant for hire +
- Resume: [http://www.jump.net/~bobp](http://www.jump.net/~bobp) +
- Email: @Bob_Pendleton +  
±-----------------------------------+

---

<div class="post-metadata">

**Author:** ![Christopher\_Thielen](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/ce73a5/32.png) [@Christopher\_Thielen](https://discourse.libsdl.org/u/Christopher_Thielen)\
**Post date:** [November 9, 2002, 12:06am UTC](https://discourse.libsdl.org/t/waiting-for-events-and-callbacks/7707/7 "2002-11-09T00:06:00Z")

</div>

ah, there’s the misunderstanding. the program isnt multithreaded at all.  
the timers are my own c code and everything runs on one thread. the  
“timer callbacks” are all my code, and SDL\_WaitEvent() is just used to  
receieve events. so then, multithreading _is_ the only way to go? i  
dunno, is that how most games do input?On Fri, 2002-11-08 at 18:55, Bob Pendleton wrote:

> On Fri, 2002-11-08 at 17:40, Chris Thielen wrote:
> 
> > On Fri, 2002-11-08 at 10:19, Bob Pendleton wrote:
> > 
> > > On Fri, 2002-11-08 at 09:10, Chris Thielen wrote:
> > > 
> > > > If I use SDL\_PushEvent() while SDL\_WaitEvent() is called, then  
> > > > SDL\_WaitEvent() will return and produce the desired effect, however, how  
> > > > do I call SDL\_PushEvent() while SDL\_WaitEvent() is running? It’s not  
> > > > multithreaded, and the timer callback is only called after  
> > > > SDL\_WaitEvent() is returned, so that would only push the event after the  
> > > > program has halted for too long on SDL\_WaitEvent(). Should the program  
> > > > be multithreaded or am I misunderstanding what you’ve said?
> > > 
> > > Use a different timer to push the event.
> > 
> > it wouldn’t matter. the code literally goes:
> > 
> > SDL\_WaitEvent()  
> > Check for timer event, if so, callback (for all timers)  
> > repeat
> 
> No, it does work. You send an event _FROM_ the timer callback. That  
> event causes WaitEvent to return. The code for the callback looks like:
> 
> Uint32 timerCallback(Uint32 interval, void \*param)  
> {  
> SDL\_Event event;
> 
> event.type = SDL\_USEREVENT;  
> event.user.code = MY\_TIMEREVENT;  
> event.user.data1 = (void \*)SDL\_GetTicks();  
> event.user.data2 = (void \*)0;
> 
> SDL\_PushEvent(&event);
> 
> return interval;  
> }
> 
> The event is generated in the timer thread, not in the main thread. Think  
> about it.

–  
Chris Thielen \<@Christopher\_Thielen\>

---

<div class="post-metadata">

**Author:** ![Matthijs\_Hollemans](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Matthijs\_Hollemans](https://discourse.libsdl.org/u/Matthijs_Hollemans)\
**Post date:** [November 9, 2002, 2:10am UTC](https://discourse.libsdl.org/t/waiting-for-events-and-callbacks/7707/8 "2002-11-09T02:10:02Z")

</div>

> > No, it does work. You send an event _FROM_ the timer callback.

> ah, there’s the misunderstanding. the program isnt multithreaded  
> at all.

As far as I understood this discussion, it does not matter whether  
you send the event from another thread, or from the event handler.  
All that matters is that the event ends up in the queue, and when  
execution reaches WaitEvent, it will see that event and process it.

> i dunno, is that how most games do input?

It looks to me you are doing things _way_ too complicated. Here is a  
typical game loop:

while (!finished)  
{  
handle\_events()  
update\_game\_world()  
draw\_screen()  
wait\_for\_next\_frame()  
SDL\_Flip()  
}

Every time through the loop you first handle the events:

```
while (SDL_PollEvent(&event))
{
    switch (event.type)
    {
        case SDL_KEYDOWN: ...
    }
}

```

Then you update the state of the game world, such as positions of  
your sprites, collision detection, etc. You can do some of this  
straight from your event handlers, too, if you want. After the game  
world has updated, you draw the visible parts of the game world on  
the screen.

I am assuming here you want a fixed framerate, of say, 50 fps. That  
means that the loop runs every 20ms. If the game logic takes less  
than that, you have some time to spare. To keep the framerate  
constant, we use wait\_for\_next\_frame(). The function looks like  
this:

```
 int32 left = next_frame_time - SDL_GetTicks();
 if (left > 10) { SDL_Delay((left/10)*10 - 1); }
 while (SDL_GetTicks() < next_frame_time) { }
 next_frame_time += MILLISECS_PER_FRAME;

```

Here, next\_frame\_time is when the next frame is supposed to happen,  
which is 20ms from now (the value of MILLISECS\_PER\_FRAME). If there  
are more than 10ms of unused time left in the current frame, we wait  
with SDL\_Delay(), which frees up time for other processes. We wait  
out the remainder in a busy loop. This is necessary because  
SDL\_Delay() typically has a granularity of 10ms, so even if you say  
SDL\_Delay(1) you will wake up 10ms later, not 1.

And that is it… There are other ways to do things, but this  
approach is probably the simplest. The example above is not  
complete – it does not handle pauzing of the game, or frames that  
take more than 20ms to complete – but it should get you started ;-)–  
Matthijs Hollemans  
All Your Software  
[www.allyoursoftware.com](http://www.allyoursoftware.com)

---

<div class="post-metadata">

**Author:** ![Daniel\_Goertzen](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/ecd19e/32.png) [@Daniel\_Goertzen](https://discourse.libsdl.org/u/Daniel_Goertzen)\
**Post date:** [November 9, 2002, 10:37am UTC](https://discourse.libsdl.org/t/waiting-for-events-and-callbacks/7707/9 "2002-11-09T10:37:01Z")

</div>

Hi Chris. I think subsequent replies have hinted at the answer but I’ll  
rehash.

When you create a timer it will generally run in a separate thread, so you  
WILL be able to call SDL\_PushEvent from your callback while your main thread  
is blocked on SDL\_WaitEvent. So effectively your program is multithreaded  
without you really intending it. You seem keen to avoid multithreading  
which is fine because it is very easy to shoot yourself in the foot. You  
have to be careful what you do in your callback, and the safest thing to do  
is just queue an event with SDL\_PushEvent and let the main thread do the  
work.

Now, did you just assume that your callback would not operate while blocked  
on SDL\_WaitEvent, or did you actually observe this behaviour? I took a  
quick look at the SDL source code and both Linux and Windows use threaded  
timers… there is no choice in this matter There does appear to be an  
option to use non-threaded timers on some platforms which could cause you  
problems, but I’m betting you’re on Linux, Windows, or some other threaded  
timer platform.

Hope I’ve set you straight.

Cheers,  
Dan.\> ----- Original Message -----

> From: [chris@luethy.net](mailto:chris@luethy.net) (Christopher Thielen)  
> To:   
> Sent: Friday, November 08, 2002 9:10 AM  
> Subject: Re: [SDL] waiting for events and callbacks
> 
> > If I use SDL\_PushEvent() while SDL\_WaitEvent() is called, then  
> > SDL\_WaitEvent() will return and produce the desired effect, however, how  
> > do I call SDL\_PushEvent() while SDL\_WaitEvent() is running? It’s not  
> > multithreaded, and the timer callback is only called after  
> > SDL\_WaitEvent() is returned, so that would only push the event after the  
> > program has halted for too long on SDL\_WaitEvent(). Should the program  
> > be multithreaded or am I misunderstanding what you’ve said?
> > 
> > On Thu, 2002-11-07 at 19:41, Daniel Goertzen wrote:
> > 
> > > In your timer callback use SDL\_PushEvent() to unblock the  
> > > SDL\_WaitEvent()  
> > > call.
> > > 
> > > Dan.
> > > 
> > > ----- Original Message -----  
> > > From: “Chris Thielen”   
> > > To:   
> > > Sent: Thursday, November 07, 2002 7:47 PM  
> > > Subject: [SDL] waiting for events and callbacks
> > > 
> > > > Hello,  
> > > > I have a problem and I was wondering if anybody could offer  
> > > > suggestions. I have a simple user interface that uses SDL to draw and  
> > > > get events. The interface uses callbacks, including callbacks for  
> > > > timers, so say, every 35 ms, it’ll issue a callback. The problem is,  
> > > > with SDL\_PollEvent(), cpu is sucked up (and adding a SDL\_Delay(1)  
> > > > fixes  
> > > > that problem but makes input response far too sluggish) but with  
> > > > SDL\_WaitEvent(), an event has to occur before SDL\_WaitEvent() returns  
> > > > and my code checks for timer callbacks … so timers are thrown off.  
> > > > Any suggestions as to how I can keep cpu usage down, keep input  
> > > > response fast, and issue the callbacks when I need?
> > > > 
> > > > –  
> > > > Chris Thielen
> > > > 
> > > > * * *
> > > > 
> > > > SDL mailing list  
> > > > SDL at [libsdl.org](http://libsdl.org)  
> > > > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
> > > 
> > > * * *
> > > 
> > > SDL mailing list  
> > > SDL at [libsdl.org](http://libsdl.org)  
> > > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)  
> > > –  
> > > Chris Thielen
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [libsdl.org](http://libsdl.org)  
> > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)

---

<div class="post-metadata">

**Author:** ![Christopher\_Thielen](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/ce73a5/32.png) [@Christopher\_Thielen](https://discourse.libsdl.org/u/Christopher_Thielen)\
**Post date:** [November 9, 2002, 11:50am UTC](https://discourse.libsdl.org/t/waiting-for-events-and-callbacks/7707/10 "2002-11-09T11:50:00Z")

</div>

Hrm, so coming from all these replies (which I thank all you guys,  
thanks Daniel, Bob, and Matthijs), I should probably just switch my  
timer code to SDL timers? The problem is I have not been using SDL  
timers, I have been keeping the time myself and checking timers and  
doing the callbacks myself. I’ll look into SDL timers instead, and sorry  
for that threading confusion.On Sat, 2002-11-09 at 10:35, Daniel Goertzen wrote:

> Hi Chris. I think subsequent replies have hinted at the answer but I’ll  
> rehash.
> 
> When you create a timer it will generally run in a separate thread, so you  
> WILL be able to call SDL\_PushEvent from your callback while your main thread  
> is blocked on SDL\_WaitEvent. So effectively your program is multithreaded  
> without you really intending it. You seem keen to avoid multithreading  
> which is fine because it is very easy to shoot yourself in the foot. You  
> have to be careful what you do in your callback, and the safest thing to do  
> is just queue an event with SDL\_PushEvent and let the main thread do the  
> work.
> 
> Now, did you just assume that your callback would not operate while blocked  
> on SDL\_WaitEvent, or did you actually observe this behaviour? I took a  
> quick look at the SDL source code and both Linux and Windows use threaded  
> timers… there is no choice in this matter There does appear to be an  
> option to use non-threaded timers on some platforms which could cause you  
> problems, but I’m betting you’re on Linux, Windows, or some other threaded  
> timer platform.
> 
> Hope I’ve set you straight.
> 
> Cheers,  
> Dan.
> 
> ----- Original Message -----  
> From: “Chris Thielen” \<@Christopher\_Thielen\>  
> To:   
> Sent: Friday, November 08, 2002 9:10 AM  
> Subject: Re: [SDL] waiting for events and callbacks
> 
> > If I use SDL\_PushEvent() while SDL\_WaitEvent() is called, then  
> > SDL\_WaitEvent() will return and produce the desired effect, however, how  
> > do I call SDL\_PushEvent() while SDL\_WaitEvent() is running? It’s not  
> > multithreaded, and the timer callback is only called after  
> > SDL\_WaitEvent() is returned, so that would only push the event after the  
> > program has halted for too long on SDL\_WaitEvent(). Should the program  
> > be multithreaded or am I misunderstanding what you’ve said?
> > 
> > On Thu, 2002-11-07 at 19:41, Daniel Goertzen wrote:
> > 
> > > In your timer callback use SDL\_PushEvent() to unblock the  
> > > SDL\_WaitEvent()  
> > > call.
> > > 
> > > Dan.
> > > 
> > > ----- Original Message -----  
> > > From: “Chris Thielen” \<@Christopher\_Thielen\>  
> > > To:   
> > > Sent: Thursday, November 07, 2002 7:47 PM  
> > > Subject: [SDL] waiting for events and callbacks
> > > 
> > > > Hello,  
> > > > I have a problem and I was wondering if anybody could offer  
> > > > suggestions. I have a simple user interface that uses SDL to draw and  
> > > > get events. The interface uses callbacks, including callbacks for  
> > > > timers, so say, every 35 ms, it’ll issue a callback. The problem is,  
> > > > with SDL\_PollEvent(), cpu is sucked up (and adding a SDL\_Delay(1)  
> > > > fixes  
> > > > that problem but makes input response far too sluggish) but with  
> > > > SDL\_WaitEvent(), an event has to occur before SDL\_WaitEvent() returns  
> > > > and my code checks for timer callbacks … so timers are thrown off.  
> > > > Any suggestions as to how I can keep cpu usage down, keep input  
> > > > response fast, and issue the callbacks when I need?
> > > > 
> > > > –  
> > > > Chris Thielen \<@Christopher\_Thielen\>
> > > > 
> > > > * * *
> > > > 
> > > > SDL mailing list  
> > > > SDL at [libsdl.org](http://libsdl.org)  
> > > > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
> > > 
> > > * * *
> > > 
> > > SDL mailing list  
> > > SDL at [libsdl.org](http://libsdl.org)  
> > > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)  
> > > –  
> > > Chris Thielen \<@Christopher\_Thielen\>
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [libsdl.org](http://libsdl.org)  
> > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
> 
> * * *
> 
> SDL mailing list  
> SDL at [libsdl.org](http://libsdl.org)  
> [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)  
> –  
> Chris Thielen \<@Christopher\_Thielen\>

---

<div class="post-metadata">

**Author:** ![Bob\_Pendleton](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@Bob\_Pendleton](https://discourse.libsdl.org/u/Bob_Pendleton)\
**Post date:** [November 11, 2002, 11:12am UTC](https://discourse.libsdl.org/t/waiting-for-events-and-callbacks/7707/11 "2002-11-11T11:12:01Z")

</div>

S’ok. no problem. YES, you should just use SDL timers. They work. The  
timer code in SDL is very good and very reliable.

```
	Bob PendletonOn Sat, 2002-11-09 at 13:41, Chris Thielen wrote:

```

> Hrm, so coming from all these replies (which I thank all you guys,  
> thanks Daniel, Bob, and Matthijs), I should probably just switch my  
> timer code to SDL timers? The problem is I have not been using SDL  
> timers, I have been keeping the time myself and checking timers and  
> doing the callbacks myself. I’ll look into SDL timers instead, and sorry  
> for that threading confusion.

±-----------------------------------+

- Bob Pendleton, consultant for hire +
- Resume: [http://www.jump.net/~bobp](http://www.jump.net/~bobp) +
- Email: @Bob_Pendleton +  
±-----------------------------------+
