clock

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 41
Reputation: BradenMurphy is an unknown quantity at this point 
Solved Threads: 0
BradenMurphy BradenMurphy is offline Offline
Light Poster

clock

 
0
  #1
Sep 10th, 2008
I got this info off a website. tells u how to make a clock in C++... can anyone tell me how to make it so the PC doesn't use 100% cpu? if u look in task manager ull see <ProjectName> is using 100% cpu
  1. // crt_clock.c
  2. // This example prompts for how long
  3. // the program is to run and then continuously
  4. // displays the elapsed time for that period.
  5. //
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10.  
  11. void sleep( clock_t wait );
  12.  
  13. int main( void )
  14. {
  15. long i = 6000000L;
  16. clock_t start, finish;
  17. double duration;
  18.  
  19. // Delay for a specified time.
  20. printf( "Delay for three seconds\n" );
  21. sleep( (clock_t)3 * CLOCKS_PER_SEC );
  22. printf( "Done!\n" );
  23.  
  24. /*
  25.   // Measure the duration of an event.
  26.   printf( "Time to do %ld empty loops is ", i );
  27.   start = clock();
  28.   while( i-- )
  29.   ;
  30.   finish = clock();
  31.   duration = (double)(finish - start) / CLOCKS_PER_SEC;
  32.   printf( "%2.1f seconds\n", duration );*/
  33. system("PAUSE");
  34. }
  35.  
  36. // Pauses for a specified number of milliseconds.
  37. void sleep( clock_t wait )
  38. {
  39. clock_t goal;
  40. goal = wait + clock();
  41. while( goal > clock() )
  42. ;
  43. }
Last edited by Narue; Sep 10th, 2008 at 2:33 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,679
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: clock

 
1
  #2
Sep 10th, 2008
>can anyone tell me how to make it so the PC doesn't use 100% cpu?
It uses 100% CPU because you're using a busy loop to pause. A true fix for the problem is "don't do that". Use some form of pause that puts the thread to sleep without affecting the other running threads. Unfortunately, there's no standard way to do that, so you'll need to look to your implementation and OS for libraries that support what you need.

For example, on Windows you might do this:
  1. #include <stdio.h>
  2. #include <windows.h>
  3.  
  4. int main ( void )
  5. {
  6. printf ( "Delay for three seconds\n" );
  7. Sleep ( 3000 );
  8. printf ( "Done!\n" );
  9. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 41
Reputation: BradenMurphy is an unknown quantity at this point 
Solved Threads: 0
BradenMurphy BradenMurphy is offline Offline
Light Poster

Re: clock

 
0
  #3
Sep 10th, 2008
ah awesome it works thx!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC