一、timer_create() + timer_settime()定时器
精确到nsec级.
1.1 main.cpp
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include<sys/time.h>
#define TIMER_SIVAL 9006
#define TIMER_START_TIME 3
#define TIMER_LOOP_TIME 1
struct timeval g_tv;
struct timezone g_tz;
void timer_thread(union sigval v)
{
struct timeval tv;
struct timezone tz;
gettimeofday (&tv , &tz);
printf("[sival:%d]sec:%ld,usec:%ld\n",v.sival_int,(tv.tv_sec - g_tv.tv_sec),(tv.tv_usec - g_tv.tv_usec));
}
int main()
{
timer_t timerid;
struct sigevent evp;
memset(&evp, 0, sizeof(struct sigevent)); //清零初始化
evp.sigev_value.sival_int = TIMER_SIVAL; //标识定时器的,回调函数可以获得
evp.sigev_notify = SIGEV_THREAD; //线程通知的方式,派驻新线程
evp.sigev_notify_function = timer_thread; //线程函数地址
if (timer_create(CL