Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create C timer in macOS

I would like to know how to create a timer in macOS / iOS. In linux you can create it using timer_create() function of time.h class but in macOS / iOS this function don't exist.

Something like NSTimer (objective-c) but in C.

Thanks

like image 383
Oriol Mari Avatar asked Jun 28 '17 15:06

Oriol Mari


People also ask

Does Macos have a built in timer?

Unlike your iPad or iPhone, MacBook doesn't have a timer. But, what do you do if you want more value out of your day? There are several ways you can use a timer on your Mac to help you focus.

Does Mac have a stopwatch?

Mac does not have its own built-in stopwatch, so you either have to get an application from the App Store or rely on running a command. Parallels Toolbox is a great choice of an application for your stopwatch needs. It has an easy-to-use stopwatch and many other useful applications as well.


1 Answers

After updating the link for Grand Central Dispatch timers (Apple's page) on a previous answer, I created some example code for two timers. It should be noted that this works on FreeBSD and MacOS, but not Linux (without GCD support). The example creates two event timers, one that fires 0.2sec and one that fires 0.5sec over a total of 20 times. There is a 1 second delay that exists before execution starts. The sleep() functions are not used.

#include <dispatch/dispatch.h>
#include <stdio.h>
#include <stdlib.h>

int i = 0;
dispatch_queue_t queue;
dispatch_source_t timer1;
dispatch_source_t timer2;


void sigtrap(int sig)
{
    dispatch_source_cancel(timer1);
    dispatch_source_cancel(timer2);
    printf("CTRL-C received, exiting program\n");
    exit(EXIT_SUCCESS);
}

void vector1(dispatch_source_t timer)
{
        printf("a: %d\n", i);
        i++;
        if (i >= 20) 
        {
            dispatch_source_cancel(timer);
        }   
}
void vector2(dispatch_source_t timer)
{
        printf("b: %d\n", i);
        i++;
        if (i >= 20)  //at 20 count cancel the 
        {
            dispatch_source_cancel(timer);
        }   
}

int main(int argc, const char* argv[]) {

    signal(SIGINT, &sigtrap);   //catch the cntl-c
    queue = dispatch_queue_create("timerQueue", 0);

    // Create dispatch timer source
    timer1 = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    timer2 = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    // Set block for dispatch source when catched events
    dispatch_source_set_event_handler(timer1, ^{vector1(timer1);});
    dispatch_source_set_event_handler(timer2, ^{vector2(timer2);});

    // Set block for dispatch source when cancel source
    dispatch_source_set_cancel_handler(timer1, ^{
        dispatch_release(timer1);
        dispatch_release(queue);
        printf("end\n");
        exit(0);
    });
    dispatch_source_set_cancel_handler(timer2, ^{
        dispatch_release(timer2);
        dispatch_release(queue);
        printf("end\n");
        exit(0);
    }); 

    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC); // after 1 sec

    // Set timer
    dispatch_source_set_timer(timer1, start, NSEC_PER_SEC / 5, 0);  // 0.2 sec
    dispatch_source_set_timer(timer2, start, NSEC_PER_SEC / 2, 0);  // 0.5 sec
    printf("start\n");    
    dispatch_resume(timer1);
    dispatch_resume(timer2);
    while(1)
    {
        ;;
    }
    return 0;
}
like image 123
b degnan Avatar answered Sep 26 '22 02:09

b degnan