Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObjC/C/C++ high resolution timer with callback

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!

like image 401
guitarflow Avatar asked Feb 23 '13 12:02

guitarflow


1 Answers

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.

like image 178
user2015453 Avatar answered Sep 28 '22 02:09

user2015453