Jump to content

(Windows) CPU Affinity


default_ex

Recommended Posts

One thing that's always annoyed me using uTorrent is that I can't permanently set the CPU Affinity. If any developer would be kind enough to include this features, below is some information on how to do it. I use this approach in my own game engine to bind threads to specific cores, so I know this method works relatively well across all version of Windows from XP to 7 (no idea on older versions).

The required functions to employ this method are the following which can be found in Kernel32.dll:

- GetCurrentProcess()

- GetCurrentThread()

- GetProcessAffinityMask(hProcess, out lpProcessAffinityMask, out lpSystemAffinityMask)

- SetThreadAffinityMask(hThread, dwThreadAffinityMask)

To gather information about the system that is required:

1) Get the process handle using GetCurrentProcess().

2) Use that to query the process and system affinity mask with GetProcessAffinityMask.

3) Perform a bitwise AND between the process and system mask to mask out any threads which may be unusable by the program.

4) At this point you have a bitmask which is marked 1 for a thread that's available or 0 for a thread that's not available.

To apply your own affinity mask:

1) Assemble a new mask using the one from above as a guide for what bits are valid.

2) Get the thread handle using GetCurrentThread().

3) Call into SetThreadAffinityMask.

4) This must be done on each thread.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...