help with "rolling my own" Timer class



Hi,

I am writing a timer class that I want to be able to get to notify me (via a callback func), when a specified interval has elapsed. I have most of the timer functionality figured - however, I need to spawn a new thread to carry out the "time watch" - and I need to do this in a cross platform (Well Linux/Windows) way ...

Any help will be much appreciated. The code (snippet) follows below:

#include <ctime>

typedef void (*TIMER_CB_FUNC)( void ) ;

class Timer {
public:
	inline Timer():m_cbfunc(0),m_interval(0),m_stime(0){} ;
	Timer( TIMER_CB_FUNC, unsigned short) ;
	Timer( const Timer&) ;
	Timer& operator= (const Timer& ) ;
	virtual ~Timer() ; //not really required

private:
	TIMER_CB_FUNC	m_cbfunc ;
	unsigned short	m_interval ;
	time_t		m_stime ;

	/* private functions */
	void reset( void );
};



Basically when the Timer class is constructed, it must start a new thread that waits till the time is up and then notifies me. MTIA

.



Relevant Pages

  • Re: Lahman, how ya doing?
    ... >> I'd started with a Timer class that keeps the total time separately in ... double event job I wound up rewriting a few member functions in the ... derived class identically except for different pointer types. ...
    (comp.object)
  • POSIX based Timer class, occasional crash
    ... I've got a Timer class and a TimerClient base class. ... number of times to fire the timer, timer attributes, a pointer to the ... client, and a pointer to a client specific "payload" that gets ...
    (comp.unix.programmer)
  • Re: Timers
    ... so do i set the interval to x amount of milliseconds based on milliseconds ... It seems to me your best bet is to determine the exact time in the future you want the timer to execute, calculate the difference between that exact time and "now", and then use the result of that as the timer delay for a one-shot timer. ... It will fire when the elapsed time has expired. ... Each timer class has a slightly different syntax for configuring it as a one-shot timer, so without knowing what class you're using, it's not possible to say for sure what you would want to do. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: when is threading better than a timer?
    ... Looks like I was using the right Timer class, ... Bob ...
    (microsoft.public.dotnet.languages.vb)
  • Re: POSIX based Timer class, occasional crash
    ... > I've got a Timer class and a TimerClient base class. ... > Every once in a while an application that is using the timer crashes, ...
    (comp.unix.programmer)