在最近的项目开发中,我发现有的人喜欢用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. 大数据技术之_09_Flume学习_Flume概述+Flume快速入门+Flume企业开发案例+Flume监控之Ganglia+Flume高级之自定义MySQLSource+Flume企业真实面试题(重点)

    第1章 Flume概述1.1 Flume定义1.2 Flume组成架构1.2.1 Agent1.2.2 Source1.2.3 Channel1.2.4 Sink1.2.5 Event1.3 Flum ...

  2. Linux中Redis的安装

    一.下载redis redis官网地址:http://www.redis.io/ 下载地址:http://download.redis.io/releases/ redis中文文档地址:http:// ...

  3. static关键字的内存分析

    通常情况下,Java把内存分为栈内存.堆内存和方法区 栈内存用来存放一些基本类型的变量和数组(数组也是一种引用类型)及对象的引用变量 堆内存主要是来放置对象的,即我们在程序中new出来的对象. sta ...

  4. 【转】Java高并发,如何解决,什么方式解决

    原文地址:https://www.cnblogs.com/lr393993507/p/5909804.html 对于我们开发的网站,如果网站的访问量非常大的话,那么我们就需要考虑相关的并发访问问题了. ...

  5. 泛型通配符extends与super的区别

    <? extends T>限定参数类型的上界:参数类型必须是T或T的子类型 <? super T> 限定参数类型的下界:参数类型必须是T或T的超类型 总结为: <? ex ...

  6. DICOM简介

    背景: DICOM分为两大类(这里只是从DICOM相关从业者日常工作角度出发来分类的):1)DICOM医学图像处理,即DCM文件中具体数据的处理,说图像可能有些狭隘,广义上还包括波形(心电).视频(超 ...

  7. JSP学习笔记(4)-Javabean

    按照sun公司的定义,Javabean是一个可重复使用的软件组件,实际上Javabean是一种Java类,通过封装属性和方法成为具有某种功能或处理某个业务的对象,简称Bean,Javabean基于ja ...

  8. ThreeJS两个点作为起始坐标画一个立方体

    drawLineBox(new THREE.Vector3(100, 50, 0), new THREE.Vector3(200, 100, 100)); function drawLineBox(s ...

  9. drupal7 模糊查询接口

    $query->condition('card_no', db_like($batch_no).'%', 'LIKE');

  10. nodejs学习 之 安装

    1. 官网找最新适合自己电脑的版本  下载  https://nodejs.org/en/download/ 2.我的是win7 x64选择了msi的安装包,安装过程修改安装的目标目录,最好不要放在c ...