实现高精度定时器

#include <windows.h>
#include <stdio.h>

/*
typedef union _LARGE_INTEGER {
	struct {
		DWORD LowPart;
		LONG HighPart;
	};
	struct {
		DWORD LowPart;
		LONG HighPart;
	} u;
	LONGLONG QuadPart;
} LARGE_INTEGER, *PLARGE_INTEGER;
*/

void DelayUs(__int64 Us)
{
    LARGE_INTEGER CurrTicks, TicksCount; 

    QueryPerformanceFrequency(&TicksCount);
    QueryPerformanceCounter(&CurrTicks); 

    TicksCount.QuadPart = TicksCount.QuadPart * Us / 1000000ll;
    TicksCount.QuadPart += CurrTicks.QuadPart; 

    while(CurrTicks.QuadPart<TicksCount.QuadPart)
        QueryPerformanceCounter(&CurrTicks);
}

__int64 __rdtsc()
{
#if 1
    __int64 val;
    __asm__ ("rdtsc" : "=A" (val));
    return val;
#else
    __int32 lo, hi;
    __asm__ __volatile__
    (
     	"rdtsc" : "=a"(lo), "=d"(hi)
    );
    return (__int64)((lo) | (hi) << 32);
#endif
}

int CPU_Frequency(void)      //CPU MHz
{
    LARGE_INTEGER CurrTicks, TicksCount;
    __int64 iStartCounter, iStopCounter;
/*
    DWORD dwOldProcessP = GetPriorityClass(GetCurrentProcess());
    DWORD dwOldThreadP = GetThreadPriority(GetCurrentThread());

    SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
*/
    QueryPerformanceFrequency(&TicksCount);
    QueryPerformanceCounter(&CurrTicks);

    TicksCount.QuadPart /= 16;
    TicksCount.QuadPart += CurrTicks.QuadPart;
/*
    __asm__ ("rdtsc");
    __asm__ ("mov DWORD PTR iStartCounter, EAX");
    __asm__ ("mov DWORD PTR (iStartCounter+4), EDX");
*/
    iStartCounter = __rdtsc();

    while(CurrTicks.QuadPart<TicksCount.QuadPart)
        QueryPerformanceCounter(&CurrTicks);
/*
    __asm__ ("rdtsc");
    __asm__ ("mov DWORD PTR iStopCounter, EAX");
    __asm__ ("mov DWORD PTR (iStopCounter + 4), EDX");
*/
    iStopCounter = __rdtsc();
/*
    SetThreadPriority(GetCurrentThread(), dwOldThreadP);
    SetPriorityClass(GetCurrentProcess(), dwOldProcessP);
*/
    return (int)(((double)iStopCounter-(double)iStartCounter)/(double)62500);
}

int main()
{
    DWORD dwOldProcessP = GetPriorityClass(GetCurrentProcess());
    DWORD dwOldThreadP = GetThreadPriority(GetCurrentThread());

    SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);

    int s = CPU_Frequency();
    printf("cpu: %d Mhz\n", s);

    __int64 k = __rdtsc();
    printf("%ld\n", (unsigned long long)k);

    __int64 iStartCounter, iStopCounter; 
    int i;
    
    for (i = 0; i < 10; i++) {
        iStartCounter = __rdtsc();
        DelayUs(50);
        //Sleep(10);
        iStopCounter = __rdtsc();
        printf("%d\n", (iStopCounter - iStartCounter) / s);
    }

    printf("ok!\n");

    SetThreadPriority(GetCurrentThread(), dwOldThreadP);
    SetPriorityClass(GetCurrentProcess(), dwOldProcessP);
    
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JAVA精确定时器,利用系统时间,使长期工作的误差稳定。 功能: ·可定时启动任务或直接启动任务 ·重复启动任务(时间间隔可在任务线程中改变,范围大于100ms,否则精度降低) 引用列表: ·import psn.razerpen.thread.AccuracyTimer; ·import psn.razerpen.thread.AccuracyTimerMission; ·import psn.razerpen.time.TimeStruct; 使用方法: //1·继承AccuracyTimerMission接口,创建一个类。 class MyTimer implements AccuracyTimerMission { //2·指定一个周期 int nDelay=1000; //3·重写run方法(如不需要使用新线程执行任务,也可留空) /** * 任务线程,本函数继承自Runnable */ @Override public void run() { System.out.println(new TimeStruct()); } //4·重写RunInCurrentThread(long nCurrentMilliSecond)方法。该方法接收当前时间,并返回下一次执行的时间。如果返回值不大于nCurrentMilliSecond则中止计时器。该方法必须重写。 /** * 接收当前时间的毫秒值,并返回下一次执行的毫秒值。如果返回的下一个时间早于当前时间,则退出 */ @Override public long RunInCurrentThread(long nCurrentMilliSecond) { return nCurrentMilliSecond+=nDelay; } } //5·创建主线程代码 public class TestTimer { public static void main(String[] args) throws InterruptedException { //6·创建一个AccuracyTimer对象,并指定一个任务。 AccuracyTimer at=new AccuracyTimer(new MyTimer()); //7·(可选)如果不需要在新线程中启动任务,则写 // at.SetNewThreadEnabled(false); //否则不写或者写 // at.SetNewThreadEnabled(true); //8·(可选)设定第一次启动的时间点SetNextMissionTime/SetNextMissionMilliSecond或延迟时间SetNextMissionMilliSecondFromNow //设置为当前这一分钟的第59秒后启动(不写此行则表示直接启动) at.SetNextMissionTime(Integer.MIN_VALUE, -1, -1, -1, -1, 59, 0); //9·启动定时器 at.Start(); //10·主线程继续 for(int i=0;i<60;++i){ Thread.sleep(1000); } //11·结束定时器 at.End(); } } 详见sample.razerpen.thread包中TestTimer及各代码文件中注释
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值