Klen
Member level 1
Hi,
I recently encountered some problem with the Sleep () in C++ ( from the windows.h header file). As per as my knowledge, Sleep ( x ) halts the program for "atleast" x miliseconds, but it could be more. As such, I know its not a good idea to use Sleep() for timing problems.
Now, I have generated a signal from a FTDI module, which has a period of 1 second, with 20ms high time, and 980ms fall time. Using some FTDI driver functions, I seemingly am able to do so. However, I wanted to check if my program actually works as expected or no, and thus I substitued the part of the code responsible for causing a 20ms delay, with Sleep(20). If my program is correct, Sleep(20) should not change anything in the program. But it did. Using GetTickCount (), I tried to see how much delay is the Sleep(20) causing, and surprisingly, it always causes a 16ms delay :shock: . This is quite interesting for me, as I always thought that
1. Sleep(20) means a delay of 20ms or more...but not less than 20
2. Sleep(x) never causes a delay of x+c such that c = constant.
Could anyone please shed some light on this behaviour of Sleep()! :?:
Thanks
PS: Sorry for the huge question, when it could have easily been put in 2 lines. I just wanted to be as detailed as possible :smile:
I recently encountered some problem with the Sleep () in C++ ( from the windows.h header file). As per as my knowledge, Sleep ( x ) halts the program for "atleast" x miliseconds, but it could be more. As such, I know its not a good idea to use Sleep() for timing problems.
Now, I have generated a signal from a FTDI module, which has a period of 1 second, with 20ms high time, and 980ms fall time. Using some FTDI driver functions, I seemingly am able to do so. However, I wanted to check if my program actually works as expected or no, and thus I substitued the part of the code responsible for causing a 20ms delay, with Sleep(20). If my program is correct, Sleep(20) should not change anything in the program. But it did. Using GetTickCount (), I tried to see how much delay is the Sleep(20) causing, and surprisingly, it always causes a 16ms delay :shock: . This is quite interesting for me, as I always thought that
1. Sleep(20) means a delay of 20ms or more...but not less than 20
2. Sleep(x) never causes a delay of x+c such that c = constant.
Could anyone please shed some light on this behaviour of Sleep()! :?:
Thanks
PS: Sorry for the huge question, when it could have easily been put in 2 lines. I just wanted to be as detailed as possible :smile: