ObjC / C / C ++ High Resolution Timer with Callback

I was sure that this question was asked earlier, so of course I used the search function to check if there is an answer that solves my problem.

However, all I could find was a lot of answers to measuring time with high accuracy.

What I need is a high-resolution timer (at least milliseconds) that allows me to activate a callback when a certain period of time has passed. I would like to use this in a Cocoa Mac OS X application, so C / C ++ or Obj-C will be possible. If additional libraries are needed, this is also good.

I found this http://www.songho.ca/misc/timer/timer.html , but it used a lively wait strategy and would cost too much performance, I guess.

Help is greatly appreciated!

+5
source share
3 answers

One great technology you can use comes directly from Apple. Sources for sending a timer:

http://developer.apple.com/library/mac/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/GCDWorkQueues/GCDWorkQueues.html

It will take some time to read this, but once you get it, it is pretty easy to use. You can specify the time interval in nanoseconds and the block that should be periodically executed.

GCD, C. Objective-C C , C. ++ , , Objective-C ++ .

+6

GCD Apple. Mac OS X GCD 10.7.

double delayInMilliseconds = 100.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInMilliseconds * NSEC_PER_MSEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    // callback invocation
});
+6

Perhaps you can use boosters,

http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/tutorial/tuttimer2.html , the tutorial gives an example in a few seconds, but you can also use milliseconds, i.e. using below ..

raise :: posix_time :: milliseconds (...)

+1
source

All Articles