• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Accuracy/Reliability of NSTimer?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accuracy/Reliability of NSTimer?


  • Subject: Re: Accuracy/Reliability of NSTimer?
  • From: Kurt Revis <email@hidden>
  • Date: Wed, 16 Oct 2002 22:06:32 -0700

On Wednesday, October 16, 2002, at 01:30 PM, Jim Vanaria wrote:

I am working on a MIDI program where I need an action to fire at a specific time (not MIDI notes/sends, I'm using coreMIDI for that), but I have read that NSTimer can "drift" from it's repeated firing frequency.

Meaning, am I guaranteed that if I ask NSTimer to fire every 0.5 seconds, will I get a firing at [exactly!] time [0, 0.5, 1, 1.5, 2, ...] (as opposed to, say: [0, 0.5, 1.01, 1.51, 2.03]) seconds? I'm not concerned with the *resolution* of the timer, just its consistency (reliability?). Obviously, drift wouldn't have much affect for times near time-zero, but will certainly be "off-beat" as time passes (time = 10mins and later).

You're on a timesharing system, so there are no guarantees that your process will ever be awakened exactly when you want it to be. This isn't unique to NSTimer; all system timers will have the same problem.

You need to set things up so that it doesn't matter exactly what time the timer fires. When the timer does fire, you need to find out what time it is (using AudioGetCurrentHostTime() or some other method) and then react appropriately. Very roughly, you can expect that most of the time the timer will fire pretty close to the time you request--maybe even early--but when the system is under load, it may be late by varying amounts.

The best thing to do is to experiment and see how accurate NSTimer really is. The results may surprise you. You might also try sleep() and usleep() and nanosleep() and mach_wait_until() and probably any number of other alternative methods.

Also note that you can change these results by changing your thread's scheduling parameters. Check the archives of this list -- we have discussed thread scheduling and priority issues a lot, although not recently.

--
Kurt Revis
email@hidden
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.

References: 
 >Accuracy/Reliability of NSTimer? (From: Jim Vanaria <email@hidden>)

  • Prev by Date: 3D Audio Unit Questions #2
  • Next by Date: Re: Synchronization problem in 10.2
  • Previous by thread: Accuracy/Reliability of NSTimer?
  • Next by thread: 3D Audio Unit Questions #2
  • Index(es):
    • Date
    • Thread