• 各平台定时器


    1Windows平台

    1.1 QueryPerformanceFrequency与QueryPerformanceCounter;

    1.2 利用CreateWaitableTimer实现纳秒级延时

    1.3 利用timeSetEvent实现1ms定时器

    2相关博客推荐

    https://blog.csdn.net/wangningyu/article/details/82051206

    https://blog.csdn.net/tobyforever/article/details/4045155

    #include "stdafx.h"
    #include "windows.h"
    #include "mmsystem.h"
    #pragma comment (lib,"winmm.lib")
    
    #define ONE_MILLI_SECOND 1 //定义1ms时钟间隔,以ms为单位 ;
    #define TWO_SECOND 2000 
    #define TIMER_ACCURACY 1 //定义时钟分辨率,以ms为单位
    
    HANDLE hEventOnTimer;
    
    void CALLBACK TimerCallback(UINT wTimerID,UINT msg,DWORD dwUser,DWORD dw1,DWORD dw2)//触发时钟
    {
     ::SetEvent(hEventOnTimer);
     printf("timer callback! at tick: %d/n",::GetTickCount());
    }
    
    void ThreadProc()   
    {
     while(1)
     {
    // ::ResetEvent(hEventOnTimer);
     ::WaitForSingleObject(hEventOnTimer,INFINITE);      // 经过测试,没有与callback函数之间没有延时
     printf("timer callback in Thread Proc! at tick: %d/n",::GetTickCount());
     }
    
    }
    
    int main()
    {
       UINT TimerID_1ms;
       TIMECAPS tc;
       DWORD ThreadID;
       hEventOnTimer=::CreateEvent(NULL,false,false,NULL);  //关键!!!
    
       //利用函数timeGetDeVCaps取出系统分辨率的取值范围,如果无错则继续; 
       if(timeGetDevCaps(&tc,sizeof(TIMECAPS))==TIMERR_NOERROR) 
       {   

          UINT wAccuracy
    =min(max(tc.wPeriodMin,TIMER_ACCURACY),tc.wPeriodMax);       //调用timeBeginPeriod函数设置定时器的分辨率       ::timeBeginPeriod(wAccuracy);   }     // ::ResetEvent(hEventOnTimer);    if ((TimerID_1ms=timeSetEvent(30,1,(LPTIMECALLBACK)TimerCallback,0,TIME_PERIODIC))==0)    {      printf("cannot set timer!/n");    };     ::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,&TimerID_1ms,0,&ThreadID);      getchar();      return 1; } ———————————————— 原文链接:https://blog.csdn.net/tobyforever/article/details/4045155

     2 linux 以及手机端

    1、gettimeofday()

    该函数把当前时间用 tv 结构体返回,当前时区的信息则放到tz所 指向的结构体。

    我们在使用该函数时,第2个参数一般为空(NULL),因为 一般只需要获取当前时间, 而不用获取时区信息。

    注意:返回的当前时间tv.tv_sec 是从1970年1月1日0 点开始的 “秒”数。

    2、clock_gettime()

    该函数是用于获取特定 时钟的时间,常用如下4种时钟

    原文链接:https://blog.csdn.net/u012351051/article/details/109372643

    #include <sys/time.h>
    int gettimeofday(struct timeval*tv, struct timezone *tz);
    其参数tv是保存获取时间结果的结构体,参数tz用于保存时区结果:
    struct timezone{
    int tz_minuteswest;/*格林威治时间往西方的时差*/
    int tz_dsttime;/*DST 时间的修正方式*/
    }
    timezone 参数若不使用则传入NULL即可。
    而结构体timeval的定义为:
    struct timeval{
    long int tv_sec; // 秒数
    long int tv_usec; // 微秒数
    }
    它获得的时间精确到微秒(1e-6 s)量级。在一段代码前后分别使用gettimeofday可以计算代码执行时间:
    struct timeval tv_begin, tv_end;
    gettimeofday(&tv_begin, NULL);
    foo();
    gettimeofday(&tv_end, NULL);
    函数执行成功后返回0,失败后返回-1,错误代码存于errno中。
    ----------
    https://blog.csdn.net/FHNCSDN/article/details/108958337
  • 相关阅读:
    idea 将java导出为可执行jar及导入jar依赖
    使用idea 调试java -jar xxx.jar方式启动
    springboot 打成的jar包在ClassLoader().getResource方法读取文件为null
    maven 使用dependencyManagement统一管理依赖版本
    Win10系列:C#应用控件基础5
    Win10系列:C#应用控件基础4
    Win10系列:C#应用控件基础3
    Win10系列:C#应用控件基础2
    Win10系列:C#应用控件基础1
    Win10系列:UWP界面布局进阶9
  • 原文地址:https://www.cnblogs.com/8335IT/p/16361201.html
Copyright © 2020-2023  润新知