http://blog.csdn.net/W511522329/archive/2010/03/06/5352597.aspx
多核时代不宜再用 x86 的 RDTSC 指令测试指令周期和时间:原因如下:RDTSC指令读取当前CPU的周期数,在多CPU系统中,这个周期数在不同的CPU之间基数不同,频率也可能不同.用从两个不同的CPU得到的周期数做计算会得出没有意义的值。如果线程在运行中被调度到了不同的CPU,就会出现上述情况。可以用SetThreadAffinityMask避免线程迁移。
虽然 RDTSC 废掉了,性能测试用的高精度计时还是有办法的 [2],在 Windows 用 QueryPerformanceCounter 和 QueryPerformanceFrequency,Linux 下用 POSIX 的 clock_gettime 函数,以 CLOCK_MONOTONIC 参数调用。
QueryPerformanceCounter() 错误的情况我们也碰见过,用 SetThreadAffinityMask() 解决。
MSDN上的描述:
1. SetThreadAffinityMask
2. The SetThreadAffinityMask function sets aprocessor affinity mask for the specified thread.
3. DWORD_PTRSetThreadAffinityMask(
4. HANDLE hThread,
5. DWORD_PTRdwThreadAffinityMask
6. );
Parameters
hThread
[in] Handle to the thread whose affinity mask is to be set.
This handle must have the THREAD_SET_INFORMATION and THREAD_QUERY_INFORMATIONaccess rights. For more information, see Thread Security and Access Rights.
dwThreadAffinityMask
[in] Affinity mask for the thread.
Windows Me/98/95: This value must be 1.
Return Values
If the function succeeds, the return value is the thread's previous affinitymask.
Windows Me/98/95: The return value is 1. To succeed, hThread must be valid anddwThreadAffinityMask must be 1.
If the function fails, the return value is zero. To get extended errorinformation, call GetLastError.
Remarks
A thread affinity mask is a bit vector in which each bit represents theprocessors that a thread is allowed to run on.
A thread affinity mask must be a proper subset of the process affinity mask forthe containing process of a thread. A thread is only allowed to run on theprocessors its process is allowed to run on.
通过调用SetThreadAffinityMask,就能为各个线程设置亲缘性屏蔽:
DWORD_PTR SetThreadAffinityMask(HANDLE hThread, DWORD_PTR dwThreadAffinityMask);
该函数中的hT h r e a d参数用于指明要限制哪个线程,dwThreadAffinityMask用于指明该线程能够在哪个CPU上运行。dwThreadAffinityMask必须是进程的亲缘性屏蔽的相应子集。返回值是线程的前一个亲缘性屏蔽。
因此,若要将3个线程限制到CPU1、2和3上去运行,可以这样操作:
1. //Thread 0 can only run on CPU 0.
2. SetThreadAffinityMask(hThread0, 0x00000001);//第0位是1
3. //Threads 1, 2, 3 run on CPUs 1, 2, 3.//第1 2 3位是1
4. SetThreadAffinityMask(hThread1, 0x0000000E);
5. SetThreadAffinityMask(hThread2, 0x0000000E);
6. SetThreadAffinityMask(hThread3, 0x0000000E);
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/W511522329/archive/2010/03/06/5352597.aspx
7. #include"stdafx.h"
8. #include<windows.h>
9. #include<string>
10. #include<iostream>
11. void running(int seconds) {
12. Sleep(seconds*1000);
13. std::cout<<"sleep for"<<seconds<<"(s)"<<std::endl;
14. }
15. int _tmain(int argc, _TCHAR* argv[])
16. {
17. SetThreadAffinityMask(GetCurrentThread(), 1);
18. LARGE_INTEGER start, end;
19. LARGE_INTEGER freq;
20. //timeConsuming();
21. QueryPerformanceFrequency(&freq);
22. QueryPerformanceCounter(&start);//start
23. std::cout<<"start.QuadPart= "<<start.QuadPart<<std::endl;//output start
24. running(10); //running 10seconds
25. QueryPerformanceCounter(&end); //end
26. std::cout<<"end.QuadPart= "<<end.QuadPart<<std::endl;//output end
27. std::cout<<"consume value= end.QuadPart - start.QuadPart = "<<(end.QuadPart -start.QuadPart)<<std::endl;
28. std::cout<<"(consumevalue/(double)freq.QuadPart) Time consumed = "<<(end.QuadPart - start.QuadPart)/(double)freq.QuadPart<<"(s)"<<std::endl; //outputconsumed time
29. return 0;
30. }