Timer or Timeout Class for Linux AND Windows ?

J

John

I'm trying to write a program that's run on both Windows 98 and Linux.

I need a timer, that I can start and when it hits a limit, it needs to
call a callback function. I also need to be able to abort the timer
before it hits the limit - it's the classic timeout type thing.

I'll readily admit I'm no expert on C++, but after a trawl of the WWW
pages, there appears to be two solutions depending on whether it's Linux
or Windows.

Surely the purpose of C++ and OOP is that we don't all keep on
reinventing the wheel, and that a Class should compile and be usable on
whichever environment we wish.

Is there a freely available Class I can download from somewhere that
will work on Linux and Windows ?

I just need to start a timer that has a callback facility, and then
stop the timer, or restart the timer.

Must exist surely.
 
G

Gernot Frisch

John said:
I'm trying to write a program that's run on both Windows 98 and
Linux.

I need a timer, that I can start and when it hits a limit, it needs
to call a callback function. I also need to be able to abort the
timer before it hits the limit - it's the classic timeout type
thing.

I'll readily admit I'm no expert on C++, but after a trawl of the
WWW pages, there appears to be two solutions depending on whether
it's Linux or Windows.

Surely the purpose of C++ and OOP is that we don't all keep on
reinventing the wheel, and that a Class should compile and be usable
on whichever environment we wish.

Is there a freely available Class I can download from somewhere that
will work on Linux and Windows ?

I just need to start a timer that has a callback facility, and then
stop the timer, or restart the timer.

Must exist surely.

There's some x-platform thread classes. Start a thread that uses time
from time.h (x-platform) to wait for a single time to be elappsed and
then call a given function.

From the scrathc of my head it might look like:

struct TIMERS
{
long nSecs;
long last;
void (*pFn)(void);
};
std::vector<TIMERS> g_timers;

void MyTimerThread
{
time_t start = time(NULL);
for(;;)
{
time_t t = time(NULL);
for(long i=0; i<g_timers.size(); ++i)
{
if (g_timers.last+g_timers.nSecs > t)
{
g_timers.last=t;
g_timers.pFn();
}
}
Sleep_some_time(); // Don't take 100% CPU power
};
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top