| | |
clock
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 41
Reputation:
Solved Threads: 0
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
c Syntax (Toggle Plain Text)
// crt_clock.c // This example prompts for how long // the program is to run and then continuously // displays the elapsed time for that period. // #include <stdio.h> #include <stdlib.h> #include <time.h> void sleep( clock_t wait ); int main( void ) { long i = 6000000L; clock_t start, finish; double duration; // Delay for a specified time. printf( "Delay for three seconds\n" ); sleep( (clock_t)3 * CLOCKS_PER_SEC ); printf( "Done!\n" ); /* // Measure the duration of an event. printf( "Time to do %ld empty loops is ", i ); start = clock(); while( i-- ) ; finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf( "%2.1f seconds\n", duration );*/ system("PAUSE"); } // Pauses for a specified number of milliseconds. void sleep( clock_t wait ) { clock_t goal; goal = wait + clock(); while( goal > clock() ) ; }
Last edited by Narue; Sep 10th, 2008 at 2:33 pm. Reason: added code tags
>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:
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:
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <windows.h> int main ( void ) { printf ( "Delay for three seconds\n" ); Sleep ( 3000 ); printf ( "Done!\n" ); }
I'm here to prove you wrong.
![]() |
Similar Threads
- Synchronize Your Computer Clock with an Internet Time Server (Windows tips 'n' tweaks)
- System clock lose about 4mins per hour!!! (Windows NT / 2000 / XP)
- Update Computer Clock Through a Firewall (Windows tips 'n' tweaks)
- Mandrake guest in VMware - System clock frozen... (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: ITS very urgents...
- Next Thread: how much to use classes
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






