|
-
Time delay C++
Hi guys, how do i create a time delay in nanoseconds / miliseconds in C++ ?
There are no stupid questions, there are stupid answers.
-
Something like this (in VC++):
Code:
//Enable the RDTSC instruction
#ifndef RDTSC
#define RDTSC __asm _emit 0x0F __asm _emit 0x31
#endif
#pragma warning(disable: 4035)
__inline __int64 GetCycleNumber(){
__asm {
RDTSC
}
}
__int64 GetHz(){
LARGE_INTEGER t1,t2,tf;
__int64 c1,c2;
QueryPerformanceFrequency(&tf);
QueryPerformanceCounter(&t1);
c1 = GetCycleNumber();
_asm {
MOV EBX, 5000000
WaitAlittle:
DEC EBX
JNZ WaitAlittle
}
QueryPerformanceCounter(&t2);
c2 = GetCycleNumber();
return ((c2 - c1) * tf.QuadPart / (t2.QuadPart - t1.QuadPart));
}
void delay(int millis){
__int64 Hz = GetHz();
__int64 start = GetCycleNumber();
while (millis * Hz / 1000 < GetCycleNumber() - start);
}
-
Anything particularly wrong with
void Sleep(DWord dwMilliseconds); in Windows.h for a Windows box
or
int nanosleep(const struct timespec *req, struct timespec *rem) in time.h for a linux box (I presume other Unix variants have something similar, take a look in time.h and see)
[EDIT]AIX has the exact same nanosleep as linux, so I expect other Unices will too [/EDIT]
[This message has been edited by Raptor^ (edited November 08, 2000).]
-
Hm is there a simpler way ? QueryPerformanceFrequency() and QueryPerformanceCounter() are not found and if i include winbase.h the debugger gives a lot of errors in that file.
------------------
What we're dealing with here is a total lack of respect for the law...
Prodigy
There are no stupid questions, there are stupid answers.
-
Sleep() from windows.h seems to work though, thanks !
There are no stupid questions, there are stupid answers.
-
erm, don't include winbase.h directly, its automatically included by windows.h and needs some of the other stuff that windows.h has.
Just include windows.h instead
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|