Specifying a Processor Affinity Bitmask

You specify processor affinity in API functions through the use of processor affinity masks. A processor affinity mask is a 64-bit value in which each bit represents a processor and the bit number (0-based) is equal to the processor number (also 0-based). A bit set to 1 means that the corresponding processor is allowed to run the process or thread, 0 means that the processor is not available.

When there is more than one bit set in the affinity mask, the process or thread is allowed to run on more than one processor. In this latter case, the RTSS scheduler will select the ideal processor or, if an ideal processor has not been defined, the processor that corresponds to the least-significant bit in the affinity mask. (There is no consideration for dynamic load balancing. See RTX64 Uses a Static Affinity Model for more information.)

For example:

To set an affinity mask for Processors 3 and 7, you would set the bits as follows:

You create the bitmask by enabling the bits that correspond to your processors. For example, the following code snippet shows how to enable bits for Processors 7 and 3:

DWORD_PTR mask = (1<< 7) + (1<< 3);

The value of (1<< 7) is 10000000 while the value of (1<< 3) is 00001000, making the sum of these two values 10001000.

To specify a mask containing the current processor your RTSS thread is running on, call the function RtGetCurrentProcessorNumber and shift as follows:

DWORD_PTR mask = 1<< RtGetCurrentProcessorNumber();

Related topics: