Skip to content

Instantly share code, notes, and snippets.

@hiroyuki-sato
Created August 6, 2019 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroyuki-sato/46723c478b4c8db5f7bdc4ad9984212d to your computer and use it in GitHub Desktop.
Save hiroyuki-sato/46723c478b4c8db5f7bdc4ad9984212d to your computer and use it in GitHub Desktop.
p64_timer_example.c
#include <p64_timer.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
void callback(p64_timer_t tim, p64_tick_t tmo, void *arg)
{
printf("fire %ld\n",time(NULL));
}
void *timer_thread(void *arg)
{
p64_tick_t tick = 1;
for (;;) {
p64_timer_tick_set(tick);
// printf("%llu\n",p64_timer_tick_get());
p64_timer_expire();
sleep(1);
tick++;
}
}
void set_sec_timer(p64_timer_t tim, unsigned int sec)
{
p64_tick_t tick;
tick = p64_timer_tick_get();
p64_timer_set(tim, tick + sec);
}
int main(){
pthread_t t;
p64_timer_t timer1;
pthread_create(&t,NULL, timer_thread, NULL);
timer1 = p64_timer_alloc(callback, NULL);
if (timer1 == P64_TIMER_NULL) {
fprintf(stderr,"p64_timer_alloc failed\n");
exit(1);
}
printf("%ld\n",time(NULL));
/* fire callback after 2 seconds */
set_timer(timer1, 2);
pthread_join(t, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment