多核CPU采用QueryPerformanceCounter计时问题

allengw 2009-07-27 07:28:59
加精
在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所述,这种计时方式是不正确的,因为在两个线程里都分别进行了计时,那么正确的应该是如何呢?谢谢
...全文
3392 32 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
cdd121 2011-10-16
  • 打赏
  • 举报
回复
学习中!!!!!!!!!!!!!!!!!!!!!
xinfengj6 2011-10-15
  • 打赏
  • 举报
回复
nice
xinfengj6 2011-10-15
  • 打赏
  • 举报
回复
看看 学习一下啊
justlikethat 2011-10-14
  • 打赏
  • 举报
回复
nice
hztj2005 2011-10-13
  • 打赏
  • 举报
回复
Set that single thread to remain on a single processor by using the Windows API SetThreadAffinityMask. Typically, this is the main game thread.
用API函数SetThreadAffinityMask,设定一个线程居留在一个处理器上(包括核,从编程上,多processor与多core没什么区别)。典型地,这个线程是主线程。(就是说锁定主线程。)

问题是其它线程如何从主线程获取时间戳。设想:用Event,一个线程a开始某一任务,SetEvent,而主线程WaitForSingleObject这个事件Event,立即用QueryPerformanceCounter 获取时间计数,传给线程a。
但是这有两个问题:
1.一个线程a,SetEvent,到主线程WaitForSingleObject这个事件Event之间,是否有时间差?
2.主线程WaitSingleFor是否会引起主线程运行滞留?
参数2,设为0表示立刻返回,而WAIT_OBJECT_0这个返回值,表示等待的目标变成激发状态,如果是WAIT_TIMEOUT表示等待时间终了。那么可以用
if(::WaitForSingleObject(a,0)==WAIT_OBJECT_0){
QueryPerformanceCounter(ta);
}
zhyzdl 2011-10-11
  • 打赏
  • 举报
回复
我是来学习的!
zmkkobe 2011-10-11
  • 打赏
  • 举报
回复
我来学习的,呵呵
家电卫士 2011-10-10
  • 打赏
  • 举报
回复
说不准,看看别人怎么说。
liu_wj 2011-10-10
  • 打赏
  • 举报
回复
学习ing
kktty 2011-10-10
  • 打赏
  • 举报
回复
不错 一定要看看
fucherlove 2011-10-10
  • 打赏
  • 举报
回复
不懂的东西太多
nettman 2011-10-10
  • 打赏
  • 举报
回复
好久的帖子又翻出来了?
xiaocongzhi 2011-10-10
  • 打赏
  • 举报
回复
这方面没怎么涉猎,进来学点!
gwemail2003 2011-10-10
  • 打赏
  • 举报
回复
怎么没有高手。。。
aydf1 2011-10-10
  • 打赏
  • 举报
回复
我是来学习的!
cnf 2011-10-10
  • 打赏
  • 举报
回复
因为要绑定CPU,这个对系统调度的影响非常大,所以要求仅仅用一个线程计时.
这个时间戳应该是个泛指,就是指queryperformancecounter的数据,真正的时间戳函数在普通应用程序中几乎没实用性。
z63030251 2011-10-09
  • 打赏
  • 举报
回复
老帖了,我遇到同样问题,百度到了这里:

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.
工作线程应该从主线程读取时间戳(timestamps),因为工作线程仅仅从主线程读取时间戳,
所以不需要使用临界区。
cdcjk 2011-10-09
  • 打赏
  • 举报
回复
英语不是很好,看不懂
stjay 2011-10-09
  • 打赏
  • 举报
回复
还有一种值得注意的情况
系统休眠后恢复,QueryPerformanceCounter计数可能会清零
shenwei35851928 2011-10-09
  • 打赏
  • 举报
回复
我是来学习的!
加载更多回复(6)

567

社区成员

发帖
与我相关
我的任务
社区描述
英特尔® 边缘计算,聚焦于边缘计算、AI、IoT等领域,为开发者提供丰富的开发资源、创新技术、解决方案与行业活动。
社区管理员
  • 英特尔技术社区
  • shere_lin
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧