Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating function invoking like NSTimer in C

I find myself in need for a repeating block of code I can execute. If I were in an object I could simply pass self to the NSTimer scheduling. I am in a pure C project at the moment and I don't see any obvious NSTimer analogs. Is there a correct way to approach this?

like image 530
Alex Bollbach Avatar asked Jun 29 '26 15:06

Alex Bollbach


1 Answers

I don't know much of anything about the foundation framework but you can hack together a timer using the mach/POSIX APIs which allow you to tap into kernel services and processes for your C application.

Using a combination of mach_absolute_time() and nanosleep() system calls may allow you to emulate a timer. It's a bit nasty if you've never done any systems programming on a *NIX like platform.

On Linux, it's much easier to implement a POSIX timer or make use of clock_gettime() and clock_nanosleep (POSIX functions) which are not available on the mac.

There's lots of info on Linux/Unix systems programming in C but not so much on a mac, so I'd recommend having a look at this question.

Also, I made my own little emulation of clock_gettime() and clock_nanosleep for the mac (shameless plug) Find it here

like image 194
ChisholmKyle Avatar answered Jul 02 '26 05:07

ChisholmKyle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!