gcc example.c -lrt -lpthread
#include
#include
#include
void
handle (sigval_t v)
{
time_t t;
char p[32];
time (&t);
strftime (p, sizeof (p), "%T", localtime (&t));
printf ("%s thread %d, val = %d, signal captured.\n", p, pthread_self (),
v.sival_int);
return;
}
int
create (int seconds, int id)
{
timer_t tid;
struct sigevent se;
struct itimerspec ts, ots;
memset (&se, 0, sizeof (se));
se.sigev_notify = SIGEV_THREAD;
se.sigev_notify_function = handle;
se.sigev_value.sival_int = id;
if (timer_create (CLOCK_REALTIME, &se, &tid) < 0)
{
perror ("timer_creat");
return -1;
}
puts ("timer_create successfully.");
ts.it_value.tv_sec = 3;
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = seconds;
ts.it_interval.tv_nsec = 0;
if (timer_settime (tid, TIMER_ABSTIME, &ts, &ots) < 0)
{
perror ("timer_settime");
return -1;
}
return 0;
}
int
main (void)
{
create (3, 1);
create (5, 2);
for (;;)
{
sleep (10);
}
}
阅读(4304) | 评论(0) | 转发(0) |