title: Implementing timer with timeout handler in C++
body: <p>I need to implement timer with timeout handler function in c++. 
  For this I am creating a timer and I am initializing <strong>sigev_notify_function</strong> of <strong>sigevent</strong> with one of the class member function. Below is the code. </p>
  
  <p><em>timer.hpp</em></p>
  
  <pre><code>#ifndef TIMERHELPER_H_
  #define TIMERHELPER_H_
  
  #include &lt;signal.h&gt;
  #include &lt;time.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  #define CLOCKID CLOCK_REALTIME
  #define SIG SIGUSR1
  
  typedef void (*TimerHandler)(sigval_t signum);
  
  class TimerTimeoutHandler
  {
      public:
          virtual void handlerFunction( void ) = 0;
  };
  
  class Timer
  {
      public:
          Timer( TimerTimeoutHandler * timeHandler );
          ~Timer();
  
          void setDuration(long int seconds);
          void start();
          void restart();
          void timeout();
          void stop();
  
      private:
          void createTimer(timer_t *timerid, TimerHandler handler_cb);
          void startTimer(timer_t timerid, int startTimeout, int cyclicTimeout);
          void stopTimer(timer_t timerid);
          void timeOutHandler( sigval_t /* signum */ );
  
          long int m_Duration;
          TimerTimeoutHandler * timeOutHandlerImp;
          timer_t timerid;
  };
  
  class TimeTimeoutHandlerImp : public TimerTimeoutHandler
  {
      public:
          TimeTimeoutHandlerImp(){}
          ~TimeTimeoutHandlerImp(){}
  
          void handlerFunction( void );
  };
  
  #endif /* TIMERHELPER_H_ */
  </code></pre>
  
  <p><em>timer.cpp</em></p>
  
  <pre><code>#include "timer.hpp"
  
  Timer::Timer( TimerTimeoutHandler * timeHandler )
  {
      timeOutHandlerImp = timeHandler;
      m_Duration = 0;
  
      createTimer( &amp;timerid, timeOutHandler );
  }
  
  Timer::~Timer()
  {
      stopTimer( timerid );
  }
  
  void Timer::setDuration(long int seconds)
  {
      m_Duration = seconds;
  }
  
  void Timer::start()
  {
      startTimer(timerid, m_Duration, 3);
  }
  
  void Timer::restart()
  {
      stopTimer(timerid);
      startTimer(timerid, m_Duration, 0);
  }
  
  void Timer::stop()
  {
      stopTimer(timerid);
  }
  
  void Timer::createTimer(timer_t *timerid, TimerHandler handler_cb)
  {
      sigevent sev;
      pthread_attr_t attr;
      pthread_attr_init( &amp;attr );
      sched_param parm;
  
      parm.sched_priority = 255;
      pthread_attr_setschedparam(&amp;attr, &amp;parm);
  
      sev.sigev_notify_attributes = &amp;attr;
      sev.sigev_notify = SIGEV_THREAD;
      sev.sigev_notify_function = handler_cb;
      sev.sigev_signo = SIG;
      sev.sigev_value.sival_ptr = timerid;
  
      timer_create(CLOCKID, &amp;sev, timerid);
  }
  
  void Timer::startTimer(timer_t timerid, int startTimeout, int cyclicTimeout)
  {
      itimerspec its;
  
      /* Start the timer */
      its.it_value.tv_sec = startTimeout;
      its.it_value.tv_nsec = 0;
  
          /* for cyclic timer */
      its.it_interval.tv_sec = cyclicTimeout;
      its.it_interval.tv_nsec = 0;
  
      timer_settime(timerid, 0, &amp;its, NULL);
  }
  
  void Timer::stopTimer(timer_t timerid)
  {
      itimerspec its;
      its.it_value.tv_sec = 0;
      its.it_value.tv_nsec = 0;
      its.it_interval.tv_sec = 0;
      its.it_interval.tv_nsec = 0;
      timer_settime(timerid, 0, &amp;its, NULL);
  }
  
  void Timer::timeOutHandler( sigval_t /* signum */ )
  {
      timeOutHandlerImp-&gt;handlerFunction();
  }
  
  
  void TimeTimeoutHandlerImp::handlerFunction( void )
  {
      cout &lt;&lt; "time handler invoked" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>But during compilation I am facing problem (this could be design issue)</p>
  
  <pre><code>g++ timer.cpp 
  timer.cpp: In constructor ‘Timer::Timer(TimerTimeoutHandler*)’:
  timer.cpp:8:43: error: no matching function for call to ‘Timer::createTimer(void**, &lt;unresolved overloaded function type&gt;)’
  timer.cpp:8:43: note: candidate is:
  In file included from timer.cpp:1:0:
  timer.hpp:35:14: note: void Timer::createTimer(void**, TimerHandler)
  timer.hpp:35:14: note:   no known conversion for argument 2 from ‘&lt;unresolved overloaded function type&gt;’ to ‘TimerHandler {aka void (*)(sigval)}’
  </code></pre>
  
  <p>Is this design in good enough to decouple timer implementation and timeout handler implementation? <em>I am using different .hpp and .cpp files for all class definition and declaration. But for simplicity I have pasted all code in two files for better understanding here</em>.</p>
  
  <p>Please advice good design for implementing the timer. Timer class should be independent of timeout handler class. Basically UNIX timer should invoke function of timeout handler class, immediately after timer expires. Please suggest.</p>