The setthreadaffinitymask function sets a processor affinity mask for the specified thread.
[Delphi]View Plaincopyprint?
- Dword_ptr Setthreadaffinitymask (
- HANDLE Hthread,
- Dword_ptr Dwthreadaffinitymask
- );
Parameters
-
Hthread
-
[In] Handle to the thread whose affinity mask are to BES set.
This handle must has 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 is 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 of vector in which each bit represents the processors that a thread was allowed to run on.
A thread affinity mask must is a proper subset of the process affinity mask for the containing process of a thread. A thread is an allowed to run on the processors it process is allowed to run on.
By calling Setthreadaffinitymask, you can set affinity masking for each thread:
[Delphi]View Plaincopyprint?
- Dword_ptr Setthreadaffinitymask (HANDLE hthread, dword_ptr dwthreadaffinitymask);
The H T H r e A D parameter in the function is used to indicate which thread to limit, and dwthreadaffinitymask is used to indicate which CPU the thread can run on. The dwthreadaffinitymask must be a corresponding subset of the affinity masks for the process. The return value is the previous affinity mask for the thread.
Therefore, to limit 3 threads to CPU1, 2, and 3, you can do this:
[Delphi]View Plaincopyprint?
- Thread 0 can only run on CPU 0.
- Setthreadaffinitymask (hThread0, 0x00000001); //No. 0 bit is 1
- Threads 1, 2, 3 run on CPUs 1, 2, 3.//1th 2 3 bit is 1
- Setthreadaffinitymask (HThread1, 0x0000000e);
- Setthreadaffinitymask (HThread2, 0x0000000e);
- Setthreadaffinitymask (HThread3, 0x0000000e);
http://blog.csdn.net/yanjiaye520/article/details/7638219
Setthreadaffinitymask Setting Thread affinity