+ 6
Time in c
I want write program that run some function after every 200 millisecond , i already search for it and read some codes but those makes me so confused, is there simple way to do it ?
8 Antworten
+ 6
Fermi
Perfect
Thank you
+ 6
Fidelis
Search those questions in Q&A and if you don't find the answer that you want then ask it in Q&A
+ 5
Aaron Eberhardt
Right , Thank you
+ 4
Ragan Murali Pasupuleti
Thanks but sleep() stop and delay other statements and i don't want it happens
I want use time.h library
+ 4
// just replace the corresponding headers and functions with their C counterpart.
#include <iostream>
#include <ctime>
int main()
{
std::clock_t t = clock();
unsigned long long i = 0;
// wrapping up everything in a loop
for (;;) {
if (clock() - t > 1000) { // executes approx every 1 second (1000ms)
std::cout << i << std::endl;
t = clock();
}
//remaining functionalities unaffected
i++;
// the rest of your code in here.
// when your code execution is complete, break
}
return 0;
}
+ 3
sleep() function from stdlib.h
+ 1
Here's a quite understandable explanation: http://www.cplusplus.com/reference/ctime/clock/
+ 1
You can basically copy most parts of the example program. But instead I'd calculate how many ticks the clock needs for 200ms before with the CLOCKS_PER_SEC constant. Then check in a loop whether the clock has reached the amount of ticks you want.