在最近的项目开发中,我发现有的人喜欢用QThread来实现需要循环执行的工作流,而有的人又喜欢用QTimer来实现。

在表面上,两种实现方式似乎都可以,但我觉得QTimer的精度可能会有问题,首先看一下别的coder关于这个问题的探索。

QTimer和Qthread的调度时间精度

http://blog.csdn.net/dijunfeng/article/details/7272475

作者:dijunfeng

最近做的一个模拟嵌入式设备的项目中,要求事件的响应精度在1毫秒左右,特地编写代码测试了一下QTimer的定时精度和QThread中的msleep()的时间精度。

QT的帮助中对于QTimer的时间精度问题是这么写的:

Timers will never time out earlier than the specified timeout value and they are not guaranteed to time out at the exact value specified.

In many situations, they may time out late by a period of time that depends on the accuracy of the system timers.

The accuracy of timers depends on the underlying operating system and hardware.

Most platforms support a resolution of 1 millisecond, though the accuracy of the timer will not equal this resolution in many real-world situations.

If Qt is unable to deliver the requested number of timer clicks, it will silently discard some.

我们的测试函数用到了windows的高精度时间读取函数,如下所示

#include <Windows.h>

#include <math.h>

#define TIMER_INTVL 1000 //毫秒

#define ARRAY_LEN 1 //数组长度

//传入调用时间间隔,打印出最大和平均时间误差

void testTimer(int intvl_us)

{

  static bool inited = false;

  static LARGE_INTEGER lastT;

  static LARGE_INTEGER freq;

  LARGE_INTEGER now;

  static int usarray[ARRAY_LEN];

  static int index = 0;

  static int maxus = 0, averus = 0, difus;//时间差

  QString info("最大时间差:");

  if(!inited)

  {

    memset(usarray, 0, sizeof(int)*ARRAY_LEN);

    QueryPerformanceCounter(&lastT);//获取第一次进入时的时间

    QueryPerformanceFrequency(&freq);//获取时钟频率

    inited = true;

    return;

  }

  QueryPerformanceCounter(&now);

  difus = ((now.QuadPart-lastT.QuadPart)*1000000)/freq.QuadPart;

  difus = abs(difus-intvl_us);

  usarray[index++] = difus;

  maxus = maxus>difus?maxus:difus;

  if(index == ARRAY_LEN)

  {

    index = 0;

    for(int i=0; i<ARRAY_LEN; i++)

      averus += usarray[i];

    averus /= ARRAY_LEN;

    info = info + QString::number(maxus) + " 平均误差 " + QString::number(averus);

    gSimDrvDlg->putInfo(info);

    maxus = 0;

    averus = 0;

  }

  lastT = now;

}

把此函数设为QTimer的超时响应函数,在32位windows7下测试QTimer的不同定时周期的调度误差如下:

1ms周期:

  最大:30、40毫秒

  平均误差:100微秒左右
 
10ms周期:
  最大:2、3毫秒,跳动比较大,也有20毫秒多过
  平均误差:200多微秒
 
100ms周期:
  最大:20多毫秒
  平均误差:10毫秒左右
 
1秒即1000ms周期
  平均误差十几毫秒
 
把此函数稍加改动,也可以放到QThread的run()函数中测试一下QThread::msleep的时间精度。
在windows下,由于操作系统的本身设计理念问题,定时器的调度误差是比较大的。

【2017-01-08】QTimer与QThread的调度时间精度的更多相关文章

  1. java编程如何实现2017-01-16 22:28:26.0这样的时间数据,转换成2017:01:16:22:28:26这样的时间数据

    不多说,直接上干货! timereplace.java package zhouls.bigdata.DataFeatureSelection.util; /* * 这个程序,是用来做补充的 */ p ...

  2. 【web开发 | 移动APP开发】 Web 移动开发指南(2017.01.05更新)

    版本记录 - 版本1.0 创建文章(2016.12.30) - 版本1.1 更正了hybird相关知识:增加了参考文章(2017.01.05): + Web APP更正为响应式移动站点与页面,简称响应 ...

  3. Mysql Innodb 性能参数设置 https://www.rathishkumar.in/2017/01/how-to-allocate-innodb-buffer-pool-size-in-mysql.html

    参考原文: https://www.rathishkumar.in/2017/01/how-to-allocate-innodb-buffer-pool-size-in-mysql.html 查看系统 ...

  4. 在不开启事件循环的线程中使用QTimer(QThread::run函数自带事件循环,在构造函数里创建线程,是一种很有意思的线程用法) good

    引入 QTimer是Qt自带的定时器类,QTimer运行时是依赖于事件循环的,简单来说,在一个不开启事件循环(未调用exec() )的线程中,QTimer是无法使用的.通过分析Qt源码可发现,调用QT ...

  5. 第08讲:Flink 窗口、时间和水印

    Flink系列文章 第01讲:Flink 的应用场景和架构模型 第02讲:Flink 入门程序 WordCount 和 SQL 实现 第03讲:Flink 的编程模型与其他框架比较 第04讲:Flin ...

  6. QTimer在QThread环境中失效的问题

    QTimer在非QThread的环境下能正常工作.但在QThread环境下,需要做一些改动才能正常工作. 创建Qt的线程有两种方式: 1. 子例化QThread 可以在虚函数run中启动定时器,大致的 ...

  7. Qt: QTimer和QThread

    让QTimer 跑在其他线程. 一般写法如下. 1. 在main thread中为worker thread指定定时器. QThread* thread = new QThread(this); th ...

  8. TIOBE.2017.01最新编程语言排行榜

    Jan 2017     Jan 2016     Change     Programming Language     Ratings     Change1    1        Java   ...

  9. pyqt QTimer,QThread例子学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import * from PyQ ...

随机推荐

  1. 使用ichartjs进行布局图表页面

    先说官网  http://www.ichartjs.com/ 进入里面会有一个可视化布局系统,布局起来自己想要的样式快捷方便,和bootstrap可视化布局系统是一个道理的.

  2. [codeup] 1943 进制转换

    题目描述 将一个长度最多为30位数字的十进制非负整数转换为二进制数输出. 输入 多组数据,每行为一个长度不超过30位的十进制非负整数.(注意是10进制数字的个数可能有30个,而非30bits的整数) ...

  3. attempt to write a readonly database错误的解决(C#,SQLite)

    今天打包WPF程序,安装后总是打不开,查看监控日志原来是SQLite的问题,报错如图 当向SQLite数据库中存入新纪录时总是显示attempt to write a readonly a datab ...

  4. oracle 合并列的函数wm_concat

    oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oracle wm_concat(column)函数实现字段合并,如果您对oracle wm_concat( ...

  5. BIO、NIO和AIO的区别

    一:事件分离器 在IO读写时,把 IO请求 与 读写操作 分离调配进行,需要用到事件分离器.根据处理机制的不同,事件分离器又分为:同步的Reactor和异步的Proactor. Reactor模型: ...

  6. 山东第四届省赛C题: A^X mod P

    http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3232 Problem C:A^X mod P Time Limit: 5 Sec  Memor ...

  7. spring-boot集成PageHelper和通用Mapper

    前提条件:已经集成mybatis 代码生成步骤: 添加依赖 <dependency> <groupId>tk.mybatis</groupId> <artif ...

  8. nginx的MainLine version、Stable version、Legacy versions

    Nginx的版本说明Mainline version:在线版本,正处于开发状态Stable version :稳定版本(一般下载使用)Legacy version :遗留版本,遗留的老的版本 Linu ...

  9. react里 MD5加密

    https://www.f2td.com/2018/11/13/encrypt-the-user-password-with-md5/

  10. JSP用户登录页面

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...