Thread.SetProcessorAffinity Method

Sets the processor affinity for a managed thread. Processor affinity determines the processors on which a thread runs. (Xbox 360 only)

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)

public void SetProcessorAffinity (
         params int[] cpus


Type: Int32

An array of identifiers that specify the hardware threads on which the managed thread is permitted to run.

For Xbox 360, only a single hardware thread can be specified—the cpus array should contain only one element. The hardware thread number must be in the range 0 through 5.

XNA Game Studio games should not use hardware threads 0 or 2; see Remarks below.

Exception typeCondition
InvalidOperationExceptionEither an invalid value was passed to the method or the thread attempting the change was different from the target thread.

Each of the three CPU cores on the Xbox 360 has two hardware threads; the hardware thread number selects a specific hardware thread on a specific core. Hardware threads 0 and 1 are the hardware threads on core 0; 2 and 3 are the hardware threads on core 1; and 4 and 5 are the hardware threads on core 2.

Hardware Thread NumberCore
0 or 10
2 or 31
4 or 52

Xbox 360 software threads are associated with, and run on, only a single hardware thread at a time. The hardware thread on which a thread runs (the thread's processor affinity) must be set to a single hardware thread, but can be subsequently changed by calling SetProcessorAffinity with a different hardware thread number. When a thread is created, it is initially assigned to the current hardware thread (that is, the hardware thread on which the current thread is running).

For Xbox 360 threads, you cannot clear the thread's processor affinity by passing an empty array as the cpus parameter. An Xbox 360 thread will always be associated with one hardware thread.

XNA Game Studio games should not use hardware threads 0 or 2, which are reserved for the XNA Framework. The following table identifies Xbox 360 hardware thread usage.

Hardware ThreadAvailable
0No, reserved for XNA Framework.
2No, reserved for XNA Framework.

To determine the number of hardware threads supported by the hardware, use System.Environment.ProcessorCount.

This method should not be used with threads created by the ThreadPool or Timer classes.

This method must only be called from the thread whose affinity is being changed. If the thread to change is not the currently executing thread, an InvalidOperationException will be thrown.

The following example shows how to change the processor affinity of the currently executing thread:

Thread.CurrentThread.SetProcessorAffinity( new int[ ] { 3 } );

Xbox 360

Community Additions