嗨,我一直在尝试使用 posix 计时器库来实现计时器,但我在实现中犯了一个错误,我使用了来自网络的示例并尝试将其封装在一个类中,但是编译器不喜欢它,基本上是在尝试分配回调函数 intsigev.sigev_notify_function = TIMER0_IRQHandler;
但我无法得到任何结果。代码如下:
类定义:
#include <sys/time.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
//se agrego para eliminar el siguiente warning del compilador
//warning: incompatible implicit declaration of built-in function 'memset'
#include <string.h> /* memset */
#include <unistd.h> /* close */
#define TIMEVAL_MAX 0xFFFFFFFF
#define TIMEVAL unsigned int
// The timer is incrementing every 4 us.
//#define MS_TO_TIMEVAL(ms) (ms * 250)
//#define US_TO_TIMEVAL(us) (us>>2)
// The timer is incrementing every 8 us.
#define MS_TO_TIMEVAL(ms) ((ms) * 125)
#define US_TO_TIMEVAL(us) ((us)>>3)
class Timer
{
public:
Timer();
void initTimer();
void setTimer(TIMEVAL aValue);
TIMEVAL getElapsedTime( void ) ;
void TIMER0_IRQHandler(sigval_t val);
private:
struct timeval last_sig;
timer_t timer;
};
以及与编译器冲突的函数:
void Timer::initTimer()
{
struct sigevent sigev;
// Take first absolute time ref.
if(gettimeofday(&last_sig,NULL)){
perror("gettimeofday()");
}
memset (&sigev, 0, sizeof (struct sigevent));
sigev.sigev_value.sival_int = 0;
sigev.sigev_notify = SIGEV_THREAD;
sigev.sigev_notify_attributes = NULL;
sigev.sigev_notify_function = &TIMER0_IRQHandler;
if( timer_create (CLOCK_REALTIME, &sigev, &timer)) {
perror("timer_create()");
}
}
*//callback function
void Timer::TIMER0_IRQHandler(sigval_t val)
{
if(gettimeofday(&last_sig,NULL)) {
perror("gettimeofday()");
}
printf("TIMER NOTIFY\n");
}
提前谢谢!