5

I was sure this question has been asked before so I did of course use the search function to check if there's an answer solving my issue.

However, all I could find were a lot of answers on measuring time in high precision.

What I would need is a high resolution (at least millisecond) timer which allows me to fire a callback when a defined time period passed. I'd like to use that in a Cocoa Mac OS X app, so C/C++ or Obj-C would be possible. If additional libraries are needed, then this is fine, too.

I found this http://www.songho.ca/misc/timer/timer.html but it's using a busy waiting strategy and would cost too much performance I assume.

Help is greatly appreciated!

guitarflow
  • 2,930
  • 25
  • 38
  • I don't know Cocoa, but you should read more about its event loop mechanism; it surely give you some timers or polling .... You basically should find out how to register a timeout event callback. – Basile Starynkevitch Feb 23 '13 at 12:10
  • This question seems to have your answer: http://stackoverflow.com/questions/464618/whats-the-equivalent-of-windows-queryperformancecounter-on-osx (second answer: On OSX mach_absolute_time and mach_timebase_info are the best equivalents to Win32 QueryPerformance* functions.) – Eli Algranti Feb 23 '13 at 12:14
  • `std::this_thread::sleep_for` comes to mind (since I happened to stumble upon it again yesterday), like, used with `std::milliseonds` (not sure if there should be a "chronos" in there or something, check it out). oh, and all the "futures" support in C++11. I haven't used it, unfortunately. – Cheers and hth. - Alf Feb 23 '13 at 12:16
  • @guitarflow do you also have accuracy/jitter requirements? – junix Feb 23 '13 at 12:24
  • 1
    well, the **traditional** way to do this in C on posix systems is using `timer_create` and signals. – Andreas Grapentin Feb 23 '13 at 13:03
  • The GCD approach advised below seems to be the best fit for me. Anyways, thanks for the suggestions! – guitarflow Feb 23 '13 at 13:15

3 Answers3

8

You can use GCD directly from Apple. On Mac OS X, GCD is available since v10.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
});
David Rodrigues
  • 844
  • 6
  • 6
7

One great technology you can use comes directly from Apple. Timer dispatch sources:

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

It will take a while to read that, but once you get it, it's quite easy to use. You can specify the time interval in nanoseconds, and the block that should be periodically executed.

It's a part of the GCD technology, and it's C based. Since Objective-C is just C plus some additions, it seems more natural to just stick to C. Going into C++ and thus Objective-C++ seems like an overkill in this case.

user2015453
  • 4,844
  • 5
  • 25
  • 27
1

May be you can use boost timers,

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

boost::posix_time::milliseconds(...)

Saqlain
  • 17,490
  • 4
  • 27
  • 33