昨日做android的时候发现android对RT模式的定时竟然支持的程度不太好,反而对setitimer形式的定时器反应正常,怪哉。
把以前的相关代码部分整理一下。
linux下使用定时器方案1: 使用setitimer。
//信号菌柄
void ProgSig_ALRM(int signo)
{
}
//注册信号句柄
void RegSigHandle()
{
sigset_t block_mask;
//屏蔽所有不用的信号
sigfillset(&block_mask);
sigdelset(&block_mask, SIGALRM);
sigprocmask(SIG_BLOCK, &block_mask, NULL);
//type 1
sigset(SIG_LOWBATTERY, ProgSig_LOWBATTERY);
sigset(SIG_SOFTPOWEROFF, ProgSig_SOFTPOWEROFF);
sigset(SIG_BATTERYOK, ProgSig_BatteryOK);
sigset(SIGCHLD, SIG_IGN);
//type 2
signal(SIG_EXITRUN, ProgSig_ExitRun);
//type3
//sigaction
struct sigaction sigact;
sigfillset(&sigact.sa_mask);
sigact.sa_handler=ProgSig_ALRM;
sigaction(SIGALRM, &sigact, NULL);
}
//创建相应的定时器
void MakeTimer()
{
//only one timer
struct itimerval itimer;
itimer.it_interval.tv_sec = 3;
itimer.it_interval.tv_usec = 0;
itimer.it_value.tv_sec = 3;
itimer.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &itimer, NULL);
}
linux下使用定时器方案2: 使用timer_create.
static timer_t time_watch;
static bool timerValid=false;
static void DO_WatchTimer(sigval_t sig);
void StopLocalPageTime()
{
if (timerValid)
{
timer_delete(time_watch);
}
timerValid = false;
}
void StartLocalPageTimer(int mSec, HWND hwnd)
{
struct sigevent timer_event;
struct itimerspec timer;
timer.it_interval.tv_sec = mSec/1000;
timer.it_interval.tv_nsec = mSec%1000;
timer.it_value = timer.it_interval;
timer_event.sigev_notify = SIGEV_THREAD;
timer_event._sigev_un._sigev_thread._function = DO_WatchTimer;
timer_event._sigev_un._sigev_thread._attribute = NULL;
timer_event.sigev_value.sival_ptr = (void *)&time_watch;
timerValid = false;
if (timer_create (CLOCK_REALTIME, &timer_event, &time_watch) < 0)
{
_DEBUG("StartLocalPageTimer: timer_create failed. \r\n", NULL);
return ;
}
if (timer_settime (time_watch, 0, &timer, NULL) < 0)
{
_DEBUG("StartLocalPageTimer: timer_settime failed. \r\n", NULL);
return ;
}
timerValid = true;
}
linux下使用定时器方案3: 使用alarm();
阅读(1924) | 评论(0) | 转发(0) |