I’m using Visual Studio 2012 and C++, I need to call a function every 5 minutes, I’ve managed to do it but it consumes 25% of my CPU which is far from ideal.
The code is as follows,
time_t start;
time_t end;
time(&start);
while (1) {
time(&end);
double dif = difftime (end,start);
if (dif >= 300) { autofunction(); time(&start);} else {} }
Is there a more CPU efficient way to go about calling a function every 5 minutes or any way to slow down my while loop?
Any guidance will be greatly appreciated.
Windows has Sleep(), and for something that happens every 300 seconds, that’s perfectly good. There are also timers in Windows, which potentially takes a timerproc argument, which is a function – but note that this is a C function, not C++, so it mustn’t be a class member function unless it is a static one.