On Windows, you can call SetProcessAffinityMask for a process and SetThreadAffinityMask for a thread. However, Windows only appears to expose GetProcessAffinityMask and not a similar API for individual threads of a process.
I have a multi-threaded program that binds individual threads to processors at run-time. As I run it, I’d like to (externally) query which threads are running on which processors to make sure it’s working correctly. I’ve written a small command-line utility to do this. But I can’t seem to find a means of finding which processor(s) or core(s) an individual thread is bound to.
This apparently must be possible; I’ve seen descriptions online of the adplus debugging utility being able to show pstack-like output to show thread affinity. And Process Explorer shows a Threads tab on multi-processor machines that shows the “Ideal Processor” of a thread.
Does anyone know how to query this piece of information?
You can do it with two calls to
SetThreadAffinityMask. This function returns the original affinity mask for the passed thread handle.So… do one call with a mask that sets affinity to one CPU, and then do a second call to restore the original mask.
Here is complete C/C++ source code including error checking:
This code probes one CPU at a time, until setting affinity works (in this case we now know the original mask!) or until the initial
1has been shifted out of theDWORD. If a CPU is asked that is not available, the function fails withERROR_INVALID_PARAMETER, and we just try the next one. Usually the first CPU will just work, so it’s reasonably efficient.If the function fails with anything other than
ERROR_INVALID_PARAMETER, it means that we either don’t have sufficient access rights to the handle, or the system is having some real problems because it can’t fulfill our request. Therefore it doesn’t make sense to continue in this case.