Thursday, July 30, 2009

Sleep(1) replacment in visual c++....?

our professor gave us a sample of recursion in c++ (in maze). since, this code is meant to be for PUTTY or Linux Redhat Terminal.....but when i try to run the code in Visual C++ compiler...it gives an error saying that sleep(1) is an undeclared identifier....i believe by having sleep(1) in the code means that Java predefined function is being called but i am not sure...is there any way that this code could run in Visual c++ too?

Sleep(1) replacment in visual c++....?
The sleep function is typically used to suspend the current thread for a given amount of time. There are two main reasons for doing this: 1) you're running a multithreading program and you want a thread to go idle for a while so that the other thread gets some CPU time to do its work, and 2) to introduce a fixed delay so that your program doesn't run too fast.





I'm guessing that your professor hasn't given you a multi-threaded program, so that line is probably in there for the second reason. If so then removing it altogether from the program will probably make it run fine, or you can add a dummy loop to burn off a few cycles. In both cases the program speed will depend on the speed of the machine you run it on.





If you really do want to add a proper sleep then it's going to depend on your target architecture, as sleep isn't standard C++ and it has a higher resolution that that provided by the standard C++ time functions. The easiest way to implement sleep is to include %26lt;windows.h%26gt; and used Sleep(1) instead. For this to work, you might need to change your Visual C++ project settings to use the multi-threaded runtime libraries instead of the single-threaded ones.





Doing it properly, i.e. for a commercial app, is usually a bit more involved. Typically you set up a loop during initialization to burn off some cycles and then count how long it took, this value is then used as the counter in the sleep function to generate a reasonably accurate sleep time. Doing it really accurately requires low-level assembly, but I'm sure that level of accuracy isn't needed in your case.
Reply:Try it like this:





Sleep(1);





...uppercase 1st letter.





Also, it's Win API(apparently)...not avail in console apps. MSVC would NOT compile with it (Sleep()) in the code of a win 32 console app, but a Win 32 GUI app did compile and run.





Here's from MSVC help:





The Sleep function suspends the execution of the current thread for a specified interval.





VOID Sleep(


DWORD dwMilliseconds // sleep time in milliseconds


);





Parameters -- dwMilliseconds


Specifies the time, in milliseconds, for which to suspend execution.





---- if that won't work for any reason, try this:





#include %26lt;time.h%26gt;


/*--------------------------------


sleep()





Works like old Borland TurboC Delay() but only if clock


granularity is millisecs (thousandths of sec)


look for CLOCKS_PER_SEC in time.h file





This function should work in millisecs in MSVC++


IE: a call like so:


sleep(1000); // yields a 1 second delay


*-------------------------------*/


void sleep(long d)


{


clock_t start=clock();





while(clock() - start %26lt; d);


}

imperial

No comments:

Post a Comment