zoukankan      html  css  js  c++  java
  • SetThreadAffinityMask设置使用多核CPU的哪个核心

    http://blog.csdn.net/solstice/archive/2010/01/16/5196544.aspx

    多核时代不宜再用 x86 的 RDTSC 指令测试指令周期和时间

    虽然 RDTSC 废掉了,性能测试用的高精度计时还是有办法的 [2],在 Windows 用 QueryPerformanceCounter 和 QueryPerformanceFrequency,Linux 下用 POSIX 的 clock_gettime 函数,以 CLOCK_MONOTONIC 参数调用。

    QueryPerformanceCounter() 错误的情况我们也碰见过,用 SetThreadAffinityMask() 解决。

    转载自:http://cooker.javaeye.com/blog/652604

    MSDN上的描述: 

    1. SetThreadAffinityMask  
    2. The SetThreadAffinityMask function sets a processor affinity mask for the specified thread.  
    3.   
    4. DWORD_PTR SetThreadAffinityMask(  
    5.   HANDLE hThread,  
    6.   DWORD_PTR dwThreadAffinityMask  
    7. );  

    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_INFORMATION access 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 affinity mask. 

    Windows Me/98/95:  The return value is 1. To succeed, hThread must be valid and dwThreadAffinityMask must be 1. 

    If the function fails, the return value is zero. To get extended error information, call GetLastError. 

    Remarks 
    A thread affinity mask is a bit vector in which each bit represents the processors that a thread is allowed to run on. 

    A thread affinity mask must be a proper subset of the process affinity mask for the containing process of a thread. A thread is only allowed to run on the processors its process is allowed to run on. 



    通过调用SetThreadAffinityMask,就能为各个线程设置亲缘性屏蔽: 

    C++代码 
    1. DWORD_PTR SetThreadAffinityMask(HANDLE hThread, DWORD_PTR dwThreadAffinityMask);  


    该函数中的h T h r e a d参数用于指明要限制哪个线程, dwThreadAffinityMask用于指明该线程能够在哪个CPU上运行。dwThreadAffinityMask必须是进程的亲缘性屏蔽的相应子集。返回值是线程的前一个亲缘性屏蔽。 

    因此,若要将3个线程限制到CPU1、2和3上去运行,可以这样操作: 

    C++代码 
    1. //Thread 0 can only run on CPU 0.  
    2.   
    3. SetThreadAffinityMask(hThread0, 0x00000001); //第0位是1  
    4.   
    5. //Threads 1, 2, 3 run on CPUs 1, 2, 3.//第1 2 3位是1  
    6.   
    7. SetThreadAffinityMask(hThread1, 0x00000002);  
    8.   
    9. SetThreadAffinityMask(hThread2, 0x00000003);  
    10.   
    11. SetThreadAffinityMask(hThread3, 0x00000004);  




    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/W511522329/archive/2010/03/06/5352597.aspx 

    C++代码 
    1. #include "stdafx.h"  
    2. #include <windows.h>    
    3. #include <string>  
    4. #include <iostream>  
    5. void running(int seconds)  {      
    6.         Sleep(seconds*1000);  
    7.         std::cout<<"sleep for "<<seconds<<"(s)"<<std::endl;  
    8. }    
    9.   
    10. int _tmain(int argc, _TCHAR* argv[])  
    11. {  
    12.     SetThreadAffinityMask(GetCurrentThread(), 1);  
    13.     LARGE_INTEGER start, end;            
    14.     LARGE_INTEGER freq;   
    15.     //timeConsuming();  
    16.     QueryPerformanceFrequency(&freq);  
    17.     QueryPerformanceCounter(&start);//start  
    18.   
    19.   
    20.     std::cout<<"start.QuadPart = "<<start.QuadPart<<std::endl;         //output start   
    21.     running(10); //running 10 seconds  
    22.     QueryPerformanceCounter(&end); //end  
    23.     std::cout<<"end.QuadPart = "<<end.QuadPart<<std::endl;    //output end   
    24.   
    25.     std::cout<<"consume value = end.QuadPart - start.QuadPart = "<<(end.QuadPart - start.QuadPart)<<std::endl;   
    26.     std::cout<<"(consume value/(double)freq.QuadPart) Time consumed = "<<(end.QuadPart - start.QuadPart)/(double)freq.QuadPart<<"(s)"<<std::endl;  //output consumed time  
    27.     return 0;  
    28. }  





    start.QuadPart = 49102789906513 
    sleep for10(s) 
    end.QuadPart = 49127801303663 
    consume value = end.QuadPart - start.QuadPart = 25011397150 
    (consume value/(double)freq.QuadPart) Time consumed = 10.0046(s)

  • 相关阅读:
    what's the python之异常处理
    what's the python之面向对象(进阶)
    what's the python之面向对象
    what's the python之自定义模块和包
    Java并发编程-并发工具包(java.util.concurrent)使用指南(全)
    Java之JUC系列:外部Tools
    java CS结构软件自动升级的实现
    史上最全最强SpringMVC详细示例实战教程
    搭建最简单的SpringMVC框架(使用maven)
    小心对待query_cache_size
  • 原文地址:https://www.cnblogs.com/kex1n/p/2040924.html
Copyright © 2011-2022 走看看