Skip to content

Instantly share code, notes, and snippets.

@pigoz
Last active April 23, 2021 03:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pigoz/5594454 to your computer and use it in GitHub Desktop.
Save pigoz/5594454 to your computer and use it in GitHub Desktop.
mach_wait_until()
#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);
}
@DragonMeistah
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment