Alternative to NSTimer
warmi Wrote:Not arbitrary but rather relatively predictable ... something NSTimer is not good for.
The iPhone/Mac OS is a preemptive multitasking system. Whatever you do in your little custom loop won't change that fact. A custom loop will not have any measurable advantage over a plain runloop with a single NSTimer on the *same thread* as the custom loop.
I also neglected to mention that if you set your NSTimer to a really small interval, but your buffer swap blocks the thread anyway, it's probably as efficient as it gets, as your timer will fire once per frame, as soon as the scheduler permits.
Anyhow, as for the screen-synced timer, that is also a bit difficult to handle. As threads are preemptive, the OS doesn't guarantee any execution time limits. Thus, even if your callback was called at the exactly right time, you could still miss the next screen refresh, if your thread didn't get enough CPU time. Temporal aliasing is unavoidable to some degree if the CPU load is high, which with a game, it typically is. Even if the amount of computation per game frame is constant, the execution time may still vary wildly. Incidentally, if you only measure time once per frame, you don't know if the culprit is that the timing is off, or that execution is taking too long. **
On a desktop Mac, input processing probably takes an order of magnitude less time to execute, due to the faster processor, etc, than on the iPhone. So if input processing on the phone takes 30% CPU time, it probably takes 3% on the desktop, and so produces much less noticable jitter, all else being constant. Also, with multiple cores, contention is even less of a problem, and even with a high load, the scheduling latency is greatly reduced.
If you had control of the thread scheduler, and a hardware interrupt for screen sync, and bounded execution time, you could probably reduce jitter a bit. However, if the amount of CPU time you use is variable, and sometimes more than what you got per screen refresh, you'll have to accept some amount of temporal aliasing.
(** if your buffer swap is synced, it'll always seem as if NSTimer skips firing some frames, eg you'll have 0.033, 0.033, 0.066, 0.033 as frame time intervals. But that isn't the fault of NSTimer, as your thread could have been blocked for 0.032s of that 0.066, simply because the buffer swap was initiated a fraction too late, and the timer can only fire after it has been completed. A custom game loop will show the exact same symptoms.)
|Messages In This Thread|
Alternative to NSTimer - DoG - May 22, 2009 01:39 AM
Possibly Related Threads...
|NSTimer fine, CADisplayLink having some issues||monteboyd||5||7,529||
Aug 31, 2010 07:05 PM
Last Post: Skorche
Sep 24, 2009 07:35 PM
Last Post: jeonghyunhan
|Using an NSTimer to progressively draw a view||StevenD||4||3,701||
May 14, 2009 07:57 AM
Last Post: StevenD
|OpenGL render loop - NSTimer vs rendering thread||smallstepforman||27||20,545||
Feb 2, 2009 10:22 AM
Last Post: ThemsAllTook
|Alternative Input/Control Ideas||chrisco||8||3,884||
Aug 6, 2008 03:21 PM
Last Post: bruss14