SetThreadIdealProcessor function (processthreadsapi.h)

Sets a preferred processor for a thread. The system schedules threads on their preferred processors whenever possible.

On a system with more than 64 processors, this function sets the preferred processor to a logical processor in the processor group to which the calling thread is assigned. Use the SetThreadIdealProcessorEx function to specify a processor group and preferred processor.

Syntax

DWORD SetThreadIdealProcessor(
  [in] HANDLE hThread,
  [in] DWORD  dwIdealProcessor
);

Parameters

[in] hThread

A handle to the thread whose preferred processor is to be set. The handle must have the THREAD_SET_INFORMATION access right. For more information, see Thread Security and Access Rights.

[in] dwIdealProcessor

The number of the preferred processor for the thread. This value is zero-based. If this parameter is MAXIMUM_PROCESSORS, the function returns the current ideal processor without changing it.

Return value

If the function succeeds, the return value is the previous preferred processor.

If the function fails, the return value is (DWORD) – 1. To get extended error information, call GetLastError.

Remarks

You can use the GetSystemInfo function to determine the number of processors on the computer. You can also use the GetProcessAffinityMask function to check the processors on which the thread is allowed to run. Note that GetProcessAffinityMask returns a bitmask whereas SetThreadIdealProcessor uses an integer value to represent the processor.

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 SetThreadIdealProcessor function sets the preferred processor to a logical processor in the thread's primary group.

To compile an application that uses this function, define _WIN32_WINNT as 0x0400 or later. For more information, see Using the Windows Headers.

Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows Server 2012 R2, and later.

Requirements

Requirement Value
Minimum supported client Windows XP [desktop apps | UWP apps]
Minimum supported server Windows Server 2003 [desktop apps | UWP apps]
Target Platform Windows
Header processthreadsapi.h (include Windows.h)
Library Kernel32.lib
DLL Kernel32.dll

See also

GetProcessAffinityMask

GetSystemInfo

Multiple Processors

OpenThread

Process and Thread Functions

SetThreadAffinityMask

SetThreadIdealProcessorEx

Threads