Last active
April 23, 2021 03:57
-
-
Save pigoz/5594454 to your computer and use it in GitHub Desktop.
mach_wait_until()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <mach/clock.h> | |
#include <mach/mach.h> | |
#include <mach/mach_time.h> | |
#include <unistd.h> | |
#include <sys/time.h> | |
#include <sys/select.h> | |
void clock_gettime(struct mach_timespec *mts) { | |
clock_serv_t cclock; | |
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); | |
clock_get_time(cclock, mts); | |
mach_port_deallocate(mach_task_self(), cclock); | |
} | |
void time_select() { | |
struct timeval wait = {0, 10e6}; | |
select(NULL, NULL, NULL, NULL, &wait); | |
} | |
void time_mach() { | |
mach_wait_until(10e6 + mach_absolute_time()); | |
} | |
int main(int argc, char **argv) { | |
struct mach_timespec start; | |
struct mach_timespec end; | |
clock_gettime(&start); | |
//time_mach(); | |
time_select(); | |
clock_gettime(&end); | |
printf("%f\n", (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 10e6); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What kind of accuracy does this have? I mean how consistent is it? If i schedule it to wait 1 second, within what margin of error will it actually fire? Thanks