C ++跨平台高分辨率定时器 [英] C++ Cross-Platform High-Resolution Timer

查看:194
本文介绍了C ++跨平台高分辨率定时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++中实现一个简单的计时器机制。代码应该在Windows和Linux中工作。分辨率应尽可能精确(至少毫秒的精度)。这将用于简单地跟踪时间的流逝,而不是实现任何类型的事件驱动设计。

解决方案

Boost.Timer 可能工作,但它取决于C函数 clock 和因此可能无法为您提供足够好的分辨率。



Boost.Date_Time包括 ptime class 。请参阅 microsec_clock :: local_time microsec_clock :: universal_time 上的文档,但请注意,Win32系统通常会不能通过此API实现微秒解析。



STLsoft 提供了其他功能, thin跨平台(Windows和Linux / Unix)围绕OS特定API的C ++包装器。其性能库有几个类,可以做你所需要的。 (要使其跨平台,请选择 winstl performance_counter > unixstl 命名空间,然后使用与您的平台匹配的命名空间。)


I'm looking to implement a simple timer mechanism in C++. The code should work in Windows and Linux. The resolution should be as precise as possible (at least millisecond accuracy). This will be used to simply track the passage of time, not to implement any kind of event-driven design. What is the best tool to accomplish this?

解决方案

Boost.Timer might work, but it depends on the C function clock and so may not have good enough resolution for you.

Boost.Date_Time includes a ptime class that's been recommended on Stack Overflow before. See its docs on microsec_clock::local_time and microsec_clock::universal_time, but note its caveat that "Win32 systems often do not achieve microsecond resolution via this API."

STLsoft provides, among other things, thin cross-platform (Windows and Linux/Unix) C++ wrappers around OS-specific APIs. Its performance library has several classes that would do what you need. (To make it cross platform, pick a class like performance_counter that exists in both the winstl and unixstl namespaces, then use whichever namespace matches your platform.)

这篇关于C ++跨平台高分辨率定时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆