Skip to content

Latest commit

 

History

History
125 lines (81 loc) · 4.8 KB

nf-winbase-setthreadaffinitymask.md

File metadata and controls

125 lines (81 loc) · 4.8 KB
UID title description helpviewer_keywords old-location tech.root ms.assetid ms.date ms.keywords req.header req.include-header req.target-type req.target-min-winverclnt req.target-min-winversvr req.kmdf-ver req.umdf-ver req.ddi-compliance req.unicode-ansi req.idl req.max-support req.namespace req.assembly req.type-library req.lib req.dll req.irql targetos req.typenames req.redist ms.custom f1_keywords dev_langs topic_type api_type api_location api_name
NF:winbase.SetThreadAffinityMask
SetThreadAffinityMask function (winbase.h)
Sets a processor affinity mask for the specified thread.
SetThreadAffinityMask
SetThreadAffinityMask function
_win32_setthreadaffinitymask
base.setthreadaffinitymask
winbase/SetThreadAffinityMask
base\setthreadaffinitymask.htm
backup
3390930d-026f-4f86-97bc-1da34bb384ba
12/05/2018
SetThreadAffinityMask, SetThreadAffinityMask function, _win32_setthreadaffinitymask, base.setthreadaffinitymask, winbase/SetThreadAffinityMask
winbase.h
Windows.h
Windows
Windows XP [desktop apps \| UWP apps]
Windows Server 2003 [desktop apps \| UWP apps]
Kernel32.lib
Kernel32.dll
Windows
19H1
SetThreadAffinityMask
winbase/SetThreadAffinityMask
c++
APIRef
kbSyntax
DllExport
Kernel32.dll
API-MS-Win-Core-ProcessTopology-Obsolete-l1-1-0.dll
API-MS-Win-Core-ProcessTopology-Obsolete-L1-1-1.dll
API-MS-Win-DownLevel-Kernel32-l2-1-0.dll
Kernel32Legacy.dll
SetThreadAffinityMask

SetThreadAffinityMask function

-description

Sets a processor affinity mask for the specified thread.

-parameters

-param hThread [in]

A handle to the thread whose affinity mask is to be set.

This handle must have the THREAD_SET_INFORMATION or THREAD_SET_LIMITED_INFORMATION access right and the THREAD_QUERY_INFORMATION or THREAD_QUERY_LIMITED_INFORMATION access right. For more information, see Thread Security and Access Rights.

Windows Server 2003 and Windows XP:  The handle must have the THREAD_SET_INFORMATION and THREAD_QUERY_INFORMATION access rights.

-param dwThreadAffinityMask [in]

The affinity mask for the thread.

On a system with more than 64 processors, the affinity mask must specify processors in the thread's current processor group.

-returns

If the function succeeds, the return value is the thread's previous affinity mask.

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

If the thread affinity mask requests a processor that is not selected for the process affinity mask, the last error code is ERROR_INVALID_PARAMETER.

-remarks

A thread affinity mask is a bit vector in which each bit represents a logical processor that a thread is allowed to run on. A thread affinity mask must be a subset of the process affinity mask for the containing process of a thread. A thread can only run on the processors its process can run on. Therefore, the thread affinity mask cannot specify a 1 bit for a processor when the process affinity mask specifies a 0 bit for that processor.

Setting an affinity mask for a process or thread can result in threads receiving less processor time, as the system is restricted from running the threads on certain processors. In most cases, it is better to let the system select an available processor.

If the new thread affinity mask does not specify the processor that is currently running the thread, the thread is rescheduled on one of the allowable processors.

Starting with Windows 11 and Windows Server 2022, on a system with more than 64 processors, process and thread affinities span all processors in the system, across all processor groups, by default. The dwThreadAffinityMask must specify processors in the thread's current primary group.

-see-also

GetProcessAffinityMask

Multiple Processors

OpenThread

Process and Thread Functions

Processor Groups

SetProcessAffinityMask

SetThreadIdealProcessor

Threads