本文介绍了在C或C亚毫秒precision时间++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存在什么技术/方法,可用于C或C ++获得亚毫秒precision计时数据,并提供什么precision和准确性?我在寻找不需要额外的硬件方法。该应用程序包括等待大约50微秒+/- 1微秒,而一些外部硬件收集数据。

What techniques / methods exist for getting sub-millisecond precision timing data in C or C++, and what precision and accuracy do they provide? I'm looking for methods that don't require additional hardware. The application involves waiting for approximately 50 microseconds +/- 1 microsecond while some external hardware collects data.

编辑:操作系统是的Wndows,可能与VS2010。如果我能得到驱动和SDK的Linux上的硬件,我可以去那里使用最新的海湾合作​​委员会。

OS is Wndows, probably with VS2010. If I can get drivers and SDK's for the hardware on Linux, I can go there using the latest GCC.

推荐答案

在使用过的,现成的操作系统打交道,准确的时间是一个非常困难的,涉及的任务。如果你真的必要保证时间,唯一真正的选择是一个完整的实时操作系统。然而,如果几乎总是是不够好,你在这里可以使用,将提供商品Windows和放大器下具有良好的精度的一些技巧; Linux的

When dealing with off-the-shelf operating systems, accurate timing is an extremely difficult and involved task. If you really need guaranteed timing, the only real option is a full real-time operating system. However if "almost always" is good enough, here are a few tricks you can use that will provide good accuracy under commodity Windows & Linux


  1. 使用一个基本上,这意味着关闭IRQ亲和所选CPU和放大器;设置处理器亲和力掩码机器上的所有其它进程忽略你的目标CPU。在您的应用程序,设置CPU的亲和力只在您的屏蔽的CPU上运行。实际上,这应该prevent操作系统从不断暂停您的应用程序,因为它总是会为CPU的唯一运行进程。

  2. 不要让让你心甘情愿的过程控制产量的操作系统(这是固有的非确定性的非实时操作系统)。没有内存分配,无插槽,不互斥,虚无缥缈。使用RDTSC在循环等待你的目标时间到达旋转。它会消耗100%的CPU,但它要走的最准确的方法。

  3. 如果2号是不是有点太龙语,你可以睡眠短,然后烧毁CPU到你的目标时间。在这里,你拿的事实,即在设定的时间间隔的CPU操作系统调度。每秒1000次每秒因您的操作系统和配置通常是100倍(在Windows中可以100 / S默认的调度周期更改为1000 / s的使用多媒体API)。这可能是一个有点难以得到正确的但本质上你需要确定OS调度期间发生的时候,并计算出你的目标之后有一个事先时间。睡眠这一持续时间,然后,在清醒,对RDTSC旋(如果你是一个单CPU上使用... QueryPerformanceCounter的或Linux等价的,如果不是),直到你的目标时间到达。偶尔,OS调度会导致你错过,但是,一般来说,这种机制的工作原理pretty不错。

  1. Use a Sheilded CPU Basically, this means turn off IRQ affinity for a selected CPU & set the processor affinity mask for all other processes on the machine to ignore your targeted CPU. On your app, set the CPU affinity to run only on your shielded CPU. Effectively, this should prevent the OS from ever suspending your app as it will always be the only runnable process for that CPU.
  2. Never allow let your process willingly yield control to the OS (which is inherently non-deterministic for non realtime OSes). No memory allocation, no sockets, no mutexes, nada. Use the RDTSC to spin in a while loop waiting for your target time to arrive. It'll consume 100% CPU but it's the most accurate way to go.
  3. If number 2 is a bit too draconic, you can 'sleep short' and then burn the CPU up to your target time. Here, you take advantage of the fact that the OS schedules the CPU at set intervals. Usually 100 times per second or 1000 times per second depending on your OS and configuration (On windows you can change the default scheduling period of 100/s to 1000/s using the multimedia API). This can be a little hard to get right but essentially you need determine when the OS scheduling periods occur and calculate the one prior to your target wake time. Sleep for this duration and then, upon waking, spin on RDTSC (if you're on a single CPU... use QueryPerformanceCounter or the Linux equivalent if not) until your target time arrives. Occasionally, OS scheduling will cause you to miss but, generally speaking, this mechanism works pretty good.

这似乎是一个简单的问题,但实现好时机获取的指数更困难更严格的时序约束。祝你好运!

It seems like a simple question, but attaining 'good' timing get's exponentially more difficult the tighter your timing constraints are. Good luck!

这篇关于在C或C亚毫秒precision时间++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:34