help with "rolling my own" Timer class

  • Thread starter Thread starter Alfonso Morra
  • Start date Start date
A

Alfonso Morra

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
 
Alfonso Morra said:
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 ...

I can't help you with the cross platform issues except to mention that there
are libraries that try to smooth over the platform differences like this one

http://www.cs.wustl.edu/~schmidt/ACE.html

and this one

http://www.roguewave.com/products/enterprise/

and this one

http://www.boost.org/doc/html/threads.html

On the Win32 side, it should be a no-brainer to wrap a class around the
multimedia function timeSetEvent() which dispatches callbacks at a future
time and which manages the lifetime of the thread on which the callbacks
occur.

Regards,
Will
 
William said:
I can't help you with the cross platform issues except to mention that there
are libraries that try to smooth over the platform differences like this one

http://www.cs.wustl.edu/~schmidt/ACE.html

and this one

http://www.roguewave.com/products/enterprise/

and this one

http://www.boost.org/doc/html/threads.html

On the Win32 side, it should be a no-brainer to wrap a class around the
multimedia function timeSetEvent() which dispatches callbacks at a future
time and which manages the lifetime of the thread on which the callbacks
occur.

Regards,
Will

Thanks Will for the post. I think to keep things simple for now, I'll
just use conditional compilation and branch the logic accross the
platforms. Would you be so kind as to get me started with the
timeSetEvent() wrapper, I'm currently reading up on the function notes
and I'm sure I'll be able to follow up on an example that you provide.

MTIA
 
Back
Top