SetThreadAffinityMask設置線程親緣性


The SetThreadAffinityMask function sets a processor affinity mask for the specified thread.

 

[delphi]  view plain copy print ?
 
  1. DWORD_PTR SetThreadAffinityMask(  
  2.   HANDLE hThread,  
  3.   DWORD_PTR dwThreadAffinityMask  
  4. );  

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,就能為各個線程設置親緣性屏蔽:

 

[delphi]  view plain copy print ?
 
  1. DWORD_PTR SetThreadAffinityMask(HANDLE hThread, DWORD_PTR dwThreadAffinityMask);  

 

該函數中的h T h r e a d參數用於指明要限制哪個線程, dwThreadAffinityMask用於指明該線程能夠在哪個CPU上運行。dwThreadAffinityMask必須是進程的親緣性屏蔽的相應子集。返回值是線程的前一個親緣性屏蔽。

因此,若要將3個線程限制到CPU1、2和3上去運行,可以這樣操作:

 

[delphi]  view plain copy print ?
 
  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, 0x0000000E);  
  8.   
  9. SetThreadAffinityMask(hThread2, 0x0000000E);  
  10.   
  11. SetThreadAffinityMask(hThread3, 0x0000000E);  

http://blog.csdn.net/yanjiaye520/article/details/7638219


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM