且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当 QueryPerformanceCounter 被调用时会发生什么?

更新时间:2021-09-13 23:51:03

Windows QueryPerformanceCounter() 具有确定处理器数量并在必要时调用同步逻辑的逻辑.它尝试使用 TSC 寄存器,但对于多处理器系统,不能保证该寄存器在处理器之间同步(更重要的是,由于智能降频和睡眠状态,可能会有很大差异).

Windows QueryPerformanceCounter() has logic to determine the number of processors and invoke syncronization logic if necessary. It attempts to use the TSC register but for multiprocessor systems this register is not guaranteed to be syncronized between processors (and more importantly can vary greatly due to intelligent downclocking and sleep states).

MSDN 说调用哪个处理器无关紧要,因此您可能会看到额外的同步代码导致这种情况的开销.另请记住,它可以调用总线传输,因此您可能会看到总线争用延迟.

MSDN says that it doesn't matter which processor this is called on so you may be seeing extra syncronization code for such a situation cause overhead. Also remember that it can invoke a bus transfer so you may be seeing bus contention delays.

如果可能,请尝试使用 SetThreadAffinityMask() 将其绑定到特定处理器.否则,您可能不得不忍受延迟,或者您可以尝试不同的计时器(例如查看 http://en.wikipedia.org/wiki/High_Precision_Event_Timer).

Try using SetThreadAffinityMask() if possible to bind it to a specific processor. Otherwise you might just have to live with the delay or you could try a different timer (for example take a look at http://en.wikipedia.org/wiki/High_Precision_Event_Timer).