Linux下使用librt中的定时器

librt是Posix的实时扩展库,下面是他内部关于定时器的接口:
timer_create
timer_delete
timer_getoverrun
timer_gettime
timer_settime

下面是例子:

#include  < iostream >
using   namespace  std;
#include 
< pthread.h >
#include 
< unistd.h >
#include 
< stdlib.h >
#include 
< signal.h >
#include 
< sys / stat.h >
#include 
< string .h >
#include 
< time.h >

#define  SIGMYTIMER (SIGRTMAX)

int  SetTimer( int  nElaspe,  int  nMode  =   1 );
void  TimerRoutine( int  signo, siginfo_t *  info,  void *  context);

int  main()
{
    cout
<<"proc id: "<<(long)getpid()<<" main thread id:"<<pthread_self()<<endl;

    
struct sigaction sysact;

    
//setup signal handler
    sigemptyset(&sysact.sa_mask);
    sysact.sa_flags 
= SA_SIGINFO;
    sysact.sa_sigaction 
= TimerRoutine;
    sigaction(SIGMYTIMER, 
&sysact, NULL);

    SetTimer(
500);
    SetTimer(
500);
    SetTimer(
500);

    
while (1)
    
{
        sleep(
1000);
    }

    cout
<<"quit!!!!"<<endl;
    
return 0;
}


timer_t IDList[
20 ];  // use to save timer id;

// mode: 0: oneshot timer; 1: periodicity timer
int  SetTimer( int  nElaspe,  int  nMode)
{
    
struct sigevent evp;

    
static int nTimerIndex = 0;

    evp.sigev_notify 
= SIGEV_SIGNAL;
    evp.sigev_signo 
= SIGMYTIMER;
    evp.sigev_value.sival_ptr 
= &IDList[nTimerIndex];
    
int nCreate = timer_create(CLOCK_REALTIME, &evp, &IDList[nTimerIndex]);

    
if (nCreate == 0//success
    {
        
struct itimerspec value;
        
struct itimerspec ovalue;

        value.it_value.tv_sec 
= nElaspe / 1000;
        value.it_value.tv_nsec 
= (nElaspe % 1000* (1000 * 1000);

        
if (nMode == 1)
        
{
            value.it_interval.tv_sec 
= value.it_value.tv_sec;
            value.it_interval.tv_nsec 
= value.it_value.tv_nsec;
        }

        
else
        
{
            value.it_interval.tv_sec 
= 0;
            value.it_interval.tv_nsec 
= 0;
        }


        
if (timer_settime(IDList[nTimerIndex], 0&value, &ovalue) == 0//success
        {
            cout
<<"Timer id:"<<IDList[nTimerIndex]<<" nElaspe:"<<nElaspe<<endl;
        }

    }

    
else
    
{
        cout
<<"create timer error"<<endl;
    }


    
//++nTimerIndex;
    return *(static_cast<int*>(IDList[nTimerIndex++]));
}


// timer singal proc
void  TimerRoutine( int  signo, siginfo_t *  info,  void *  context)
{
    
if (signo != SIGMYTIMER)
        
return;

    
//display time
    time_t currtime;
    time(
&currtime);
    tm
* pTm = localtime(&currtime);
    
if (pTm)
    
{
        
char sBuf[30];
        sprintf(sBuf, 
"%02d:%02d:%02d", pTm->tm_hour, pTm->tm_min, pTm->tm_sec);
        cout
<<sBuf;
    }


    cout
<<" timer_id:"<<*(int*)(info->si_value.sival_ptr)<<" sig_id:"<<signo
            
<<" thread_id:"<<pthread_self()<<endl;
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值