jaydnul Posted December 2, 2014 Posted December 2, 2014 All I need is to run a certain section of my code (that loops forever) for a certain amount of time, about 10 seconds. The obvious problem is when it gets to that section, it loops forever and doesn't move on. Is there a function that will allow me to run a given chunk for a given amount of time? Thanks
Sensei Posted December 2, 2014 Posted December 2, 2014 (edited) Windows OS has Sleep() function. http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx For portable C/C++ you can use time() and/or clock() functions in loop and looking for certain increase of result. But this way CPU will be 100% busy while waiting. Sleep() will put thread to sleep taking 0% CPU. Edited December 2, 2014 by Sensei
fiveworlds Posted December 2, 2014 Posted December 2, 2014 (edited) Of course there is runs for 10 seconds. #include <iostream> #include <string> #include <stdio.h> #include <time.h> int main() { int b=time(0)+10; while(b>time(0)) { std::cout << time(0) << std::endl; } } Edited December 2, 2014 by fiveworlds 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now