FlightGear Newsletter January 2014: Difference between revisions

Jump to navigation Jump to search
Line 32: Line 32:
Continue reading at [[Soaring instrumentation sdk]]...
Continue reading at [[Soaring instrumentation sdk]]...
=== Behind the Scenes of: Nasal Loops ===
=== Behind the Scenes of: Nasal Loops ===
Nasal has several ways to implement an iteration.
Nasal has several ways to implement an iteration, including repeated events run from listeners or timers.
A polling loop is akin to somebody permanently running to a room to check if the lights are on - a listener is like somebody being INSIDE the room SLEEPING and only WAKING up once the lights are turned on.
A polling loop, run via a timer, is akin to somebody permanently running to a room to check if the lights are on - a listener is like somebody being INSIDE the room SLEEPING and only WAKING up once the lights are turned on.
The setlistener API is intended to catch rare events. Avoid complex loops if you don't have to.
The setlistener API is intended to catch rare events, whereas timers are run often, pretty much regardless of external events. Both are triggered by calling a so-called "callback" - which is just a provided function to be called.
In general, "loops" are not bad or expensive, it really depends on what you're doing inside the loop. A loop will be executed within a single frame normally - so a long-running loop will add up to the frame spacing. There's nothing magic about timers or listeners - they can just as well inflate your frame spacing. It doesn't matter if the code/callback is run inside a loop, timer or a listener - what matter is the complexity of the code that runs. timers or listeners are only really preferable over loops when it comes to checking for some condition, because polling is called "busy-waiting", i.e. more expensive, see my previous analogy. A listener or timer "waiting" is not resource-hungry, it's not even busy - it's not doing anything until it is "fired". Regarding setprop/getprop - they're not as bad as we used to think - in fact, Thorsten has shown that they're preferable over most props.nas APIs, this may however change once the whole thing is replaced with cppbind bindings.
 
Well loops aren't bad necessarily: they can be used in a less-than-optimal manner, but there are often times where they make a lot of sense.
In general, timers are not bad or expensive, because it really depends on what you're doing inside the, but some constructs benefit from listeners. Any callback will be executed within a single frame normally - so a long-running timer will add up to the frame spacing, as will a listener triggered just as often or even multiple times per frame. It doesn't matter if the code/callback is run inside a timer or a listener - what matter is the complexity of the code that runs. Ultimately, though, some code has to be executed somewhere to get the desired result, but in most cases that code can be simple rather than complex. Listeners are only really preferable over timers when it comes to checking for some condition, because timerloop-based polling is called "busy-waiting", i.e. more expensive, see the previous analogy. A listener on the other hand when it is "waiting" is not resource-hungry, it's not even busy - it's just not doing anything until it is "fired". However, there are often times where loops make a lot of sense, mainly when lots of values of properties need to be used to drive or evaluate a subsystem or equation.


Continue reading at [[Nasal Loops]]...
Continue reading at [[Nasal Loops]]...
395

edits

Navigation menu