Time delay C++

Sharky Forums


Results 1 to 6 of 6

Thread: Time delay C++

  1. #1
    Tiger Shark
    Join Date
    Oct 2000
    Location
    Phila, PA
    Posts
    519

    Post 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.

  2. #2
    Hammerhead Shark
    Join Date
    Sep 2000
    Location
    Luleå, Sweden
    Posts
    1,921

    Post

    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);
    }
    Get UniTuner here

  3. #3
    Catfish
    Join Date
    Sep 2000
    Location
    Southampton, UK
    Posts
    113

    Post

    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).]
    UnrealFortress Developer

  4. #4
    Tiger Shark
    Join Date
    Oct 2000
    Location
    Phila, PA
    Posts
    519

    Post

    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.

  5. #5
    Tiger Shark
    Join Date
    Oct 2000
    Location
    Phila, PA
    Posts
    519

    Post

    Sleep() from windows.h seems to work though, thanks !
    There are no stupid questions, there are stupid answers.

  6. #6
    Catfish
    Join Date
    Sep 2000
    Location
    Southampton, UK
    Posts
    113

    Post

    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
    UnrealFortress Developer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •