<div dir="ltr"><br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra">Okay, it wasn't that easy. I *do* need to have a Gambit thread running a loop in the background as well. How can I keep that loop running?<br>
</div></div></blockquote><div><br></div><div>What do you need to do in this thread exactly?<br><br></div><div>I imagine that loop will keep running as long as you are in the Scheme world, that is, handling any type of event.  Unfortunately, in an event-based system your app will by default spend much of the time in the C world, waiting for things to happen.  A way to work around that is suggested below.<br>
</div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"></div><div class="gmail_extra">


When you said "install a timer in the host system and keep calling" you mean that I need to break up that loop into a callback which is called with a repeating timer? (I think this is not a good solution).</div>
</div></blockquote></div><div class="gmail_quote"><br><div>If you really prefer to implement your background task as a loop in a gambit thread, you can communicate between the 'main' (event-system controlled) and 'worker' gambit threads via gambit thread mailboxes.  So your timer handler just sends a message to the worker thread and waits for a message in response.  So the worker loop blocks waiting for a message at the beginning of each iteration and sends a message back at the end.  The content of these messages can be bogus and ignored both ways.  What this achieves is setting apart some time to give your logic a chance to run.<br>
</div><div><br>Also, many event-based systems have an 'on idle' event type such that if you register to listen for it, your handler will get triggered whenever the application is doing nothing.  So if you don't really need your background logic to happen in regularly timed ticks, using the idle handler instead might make better use of processing time left over from event handling and rendering.<br>
<br></div><div>As a step further in this direction, you can have *all* your event handlers works like this, just delegating the real work to the "worker" gambit thread through mailboxes, and then you can make your code feel pretty much as though you controlled the main event loop.  In this case, the messages sent to mailboxes would contain information about the events.  So the worker thread can switch over event type, just as in plain SDL.  Note you still need a timer or idle handler to have stuff happen outside of user input and redraw events.<br>
<br>In this case your "worker" thread looks and feels pretty much like a "main" thread.  The event handlers themselves only contain stubs.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div class="gmail_extra"></div>

<div class="gmail_extra">
And then, where would you create and start that thread?<br></div></div></blockquote><div><br></div><div>I don't know the specifics of your platform.  In some, starting the event loop is done explicitly, so you can create the thread before you do that.  In others, there is an 'init' event type.  Where would you, for example, initialize OpenGL state that will not change throughout your application?  I bet somewhere around there you might be able to launch that thread too.<br>
<br>As a last resort, you can just do it the first time that any event handler gets called.<br></div><div><br> </div></div></div></div>