linux - c++ timer implemetation - Assigning a member function to signal callback function pointer -


hi have been tryng implement timer using posix timer libs, making mistake in implemetation, using example web , tryng encapsulating in class but, compiler doesnt it, tryng asign callback function int sigev.sigev_notify_function = timer0_irqhandler; cannot result. here goes code:

the class definition:

#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 // timer incrementing every 4 us. //#define ms_to_timeval(ms) (ms * 250) //#define us_to_timeval(us) (us>>2)  // timer 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;  }; 

and function conflicting compiler:

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"); } 

thx in advance!

to call member function, need pointer this, means can't directly. can, however, use static function wrapper callback, can extract this pointer , call real callback:

class timer { public:     static void handler_wrapper(sigval_t val);     void handler(); };  void timer::handler_wrapper(sigval_t val) {     timer *object = (timer *)val.sival_ptr;     object->handler(); }  void timer::handler(void) {     // whatever.  remember thread context you're in }  // in main sigev.sigev_value.sival_ptr = (void*) this; sigev.sigev_notify_function = &timer::handler_wrapper; 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -