简体   繁体   English

如何在 C 中实现用户模式定时器?

[英]How to implement usermode timer in C?

How we can implement our own timer function in Windows without using Library functions?我们如何在不使用库函数的情况下在 Windows 中实现自己的定时器 function? Should we deal with assembly language instructions?我们应该处理汇编语言指令吗?

This depends on the precision and resolution you require.这取决于您需要的精度和分辨率。

Assuming you need more than second precision, it's popular to wrap/use the RDTSC instruction.假设您需要超过秒的精度,包装/使用RDTSC指令很流行。 But you can also just use the Win32 API, the QueryPerformanceCounter() is widely used for this.但是您也可以只使用 Win32 API, QueryPerformanceCounter()被广泛用于此。

Many APIs like Intel's Threading Building Blocks ( http://threadingbuildingblocks.org ), OpenMP ( http://openmp.org ), SDL ( http://libsdl.org ) or SFML ( http://sfml-dev.org ), and I think also OpenGL ( http://opengl.org ) implement a timer or a wrapper around a timer. Many APIs like Intel's Threading Building Blocks ( http://threadingbuildingblocks.org ), OpenMP ( http://openmp.org ), SDL ( http://libsdl.org ) or SFML ( http://sfml-dev.org ),而且我认为 OpenGL ( http://opengl.org ) 也实现了定时器或定时器的包装器。

Saying "without using Library functions" is a bit vague: Which library do you mean specifically?说“不使用库函数”有点含糊:你具体指的是哪个库? In the end, you have to use some library function, or inline assembly, because C++ doesn't have timing functionality in the core language.最后,您必须使用一些库 function 或内联汇编,因为 C++ 在核心语言中没有计时功能。 And without any timer processor or some other timing hardware in whichever form (*), there is no way to tell the time, not vaguely, not exactly.如果没有任何计时器处理器或其他任何形式的计时硬件 (*),就无法告诉时间,不是模糊的,不是准确的。

(*) That includes internet time providers. (*) 这包括互联网时间提供商。

get the atomclock time from an internet time service.从互联网时间服务获取原子钟时间。

else you will always deal with some sort of library-functions with which you can talk to the BIOS, which is the part that makes the tick-tock from the Quartz visible to the OS.否则,您将始终处理可以与 BIOS 对话的某种库函数,这是使 Quartz 的滴答声对操作系统可见的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM