ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

各平台定时器

2022-07-03 16:31:47  阅读:258  来源: 互联网

标签:定时器 tz struct tv 平台 int NULL hEventOnTimer


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

 

标签:定时器,tz,struct,tv,平台,int,NULL,hEventOnTimer
来源: https://www.cnblogs.com/8335IT/p/16361201.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有