"John" <no****@nospam.com> schrieb im Newsbeitrag
news:CI****************@newsfe1-win.ntli.net...
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[i].last+g_timers[i].nSecs > t)
{
g_timers[i].last=t;
g_timers[i].pFn();
}
}
Sleep_some_time(); // Don't take 100% CPU power
};
}