Is there an alternative for sleep() in C?

后端 未结 16 1952
无人及你
无人及你 2020-12-02 14:55

In traditional embedded programming, we will give a delay function like so:

for(i=0;i<255;i++)
   for(j=0;j<255;j++);

In the micropro

16条回答
  •  庸人自扰
    2020-12-02 15:27

    #include 
    
    static NTSTATUS(__stdcall *NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterval) = (NTSTATUS(__stdcall*)(BOOL, PLARGE_INTEGER)) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtDelayExecution");
    
    static NTSTATUS(__stdcall *ZwSetTimerResolution)(IN ULONG RequestedResolution, IN BOOLEAN Set, OUT PULONG ActualResolution) = (NTSTATUS(__stdcall*)(ULONG, BOOLEAN, PULONG)) GetProcAddress(GetModuleHandle("ntdll.dll"), "ZwSetTimerResolution");
    
    
    
    
    static void SleepShort(float milliseconds) {
        static bool once = true;
        if (once) {
            ULONG actualResolution;
            ZwSetTimerResolution(1, true, &actualResolution);
            once = false;
        }
    
        LARGE_INTEGER interval;
        interval.QuadPart = -1 * (int)(milliseconds * 10000.0f);
        NtDelayExecution(false, &interval);
    }
    

提交回复
热议问题