多核CPU采用QueryPerformanceCounter计时问题
在MSDN里读到了一段话,不太明白,在此询问各位。
http://msdn.microsoft.com/en-us/library/bb173458(VS.85).aspx
在上述的解释中,提到了用QueryPerformanceCounter计时的一些问题,在微软推荐的理由步骤中,有下面一段话:
“Compute all timing on a single thread. Computation of timing on multiple threads — for example, with each thread associated with a specific processor — greatly reduces performance of multi-core systems.
Set that single thread to remain on a single processor by using the Windows API SetThreadAffinityMask. Typically, this is the main game thread. While QueryPerformanceCounter and QueryPerformanceFrequency typically adjust for multiple processors, bugs in the BIOS or drivers may result in these routines returning different values as the thread moves from one processor to another. So, it's best to keep the thread on a single processor.
All other threads should operate without gathering their own timer data. We do not recommend using a worker thread to compute timing, as this will become a synchronization bottleneck. Instead, worker threads should read timestamps from the main thread, and because the worker threads only read timestamps, there is no need to use critical sections.
”
文中说道,在一个单独的线程中计算时间。其它线程可以用获取时间戳的方法获得自己的时间数据。这个应该怎么实现呢?
比如,我现在有下面这样一个程序:
Thread1()
{
//线程1函数
start = QueryPerformanceCounter();
fun1();
end = QueryPerformanceCounter-start;
};
Thread2()
{
//线程2函数
start = QueryPerformanceCounter();
fun2();
end = QueryPerformanceCounter-start;
};
main()
{
CreatThread1();//创建线程1
CreatTrhead2();//创建线程2
}
如果按照MSDN所述,这种计时方式是不正确的,因为在两个线程里都分别进行了计时,那么正确的应该是如何呢?谢谢