QueryPerformanceCounter에 숨겨진 비밀 하나…

@codemaru · May 19, 2010 · 1 min read

정밀한 시간을 측정할 때 QueryPerformanceCounter라는 함수를 많이 사용합니다. 그런데 이 놈에 숨겨진 사실이 하나 있습니다. QueryPerformanceCounter 내부적으로 CPU 클럭 카운터를 사용하는데 이 놈이 멀티 코어에서는 동기화가 되지 않는 경우가 있다고 합니다. 즉, A라는 스레드가 QueryPerformanceCounter를 사용해서 시간을 측정한다고 할 때, 처음은 1번 CPU에서 구하고 다음은 2번 CPU에서 값을 구한다면 그 시간 차이가 이상한 값일 수 있다는 이야입니다. 이런 것을 보정하기 위해서는 시간 측정 스레드를  SetThreadAffinityMask 함수를 사용해서 특정 CPU에 고정시켜 놓고 사용하는 것이 안전합니다.

4.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.

http://msdn.microsoft.com/en-us/library/ee417693.aspx

@codemaru
돌아보니 좋은 날도 있었고, 나쁜 날도 있었다. 그런 나의 모든 소소한 일상과 배움을 기록한다. 여기에 기록된 모든 내용은 한 개인의 관점이고 의견이다. 내가 속한 조직과는 1도 상관이 없다.
(C) 2001 YoungJin Shin, 0일째 운영 중