0

Hello DaniWeb,

So I have a function:

time_t current_time = time( NULL );
while ( 1 )
      if ( time( NULL ) == current_time + 3600 ) // execute body every hour
      {
        // stuff
        current_time = time( NULL );
      }

This is the only way I could think of doing it, but it just doesn't seem right to have to use while(1) . When I look at the processor's activity, it is hogging a lot of CPU power and making tons of system calls probably for time() , since it has to check the condition over and over and over.

Is there a less expensive way to do this? Thank you in advance

4
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by omri374
0

If you only want it to do something every hour, then put it to sleep for an hour - this is MS-Windows, *nix would be sleep() with lower-case 's'.

while ( 1 )
{
    Sleep(60*60*1000); // sleep for one hour
    // stuff
}
0

seems like Microsoft always has to do things slightly differently... thanks again Ancient Dragon, that was exactly what I was looking for, reduced CPU usage significantly

0

You could also setup a timer, that will call a callback and/or send a windows message.

Hi,
Could you please exaplain a bit about how to solve this using a timer?

many thanks!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.