Skip to content

Instantly share code, notes, and snippets.

@yamamaya
Created April 1, 2014 09:35
Show Gist options
  • Save yamamaya/9910970 to your computer and use it in GitHub Desktop.
Save yamamaya/9910970 to your computer and use it in GitHub Desktop.
#include <inttypes.h>
#include <pthread.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void TimerHandler( union sigval sv ) {
printf( "TimerHandler!\n" );
}
int main() {
timer_t timer;
struct sigevent sev;
memset( &sev, 0, sizeof( sev ) );
sev.sigev_value.sival_int = 1;
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = TimerHandler;
sev.sigev_notify_attributes = NULL;
timer_create( CLOCK_REALTIME, &sev, &timer );
struct itimerspec its;
memset( &its, 0, sizeof( its ) );
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = 500L*1000*1000; // 500ms
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
timer_settime( timer, 0, &its, NULL );
while ( 1 ) {
sleep( 1 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment