Hi,
In a multi-threaded app on a dual core machine, is it possible to
know on which cpu a particular thread is currently executing on?
thanks,
Arun
You can call (through PInvoke) ntdll.dll function NtGetCurrentProcessor, on
Vista and later, you'll have to call kernel32 GetCurrentProcessor.
On down-level windows:
[DllImport("ntdll"), SuppressUnmanagedCodeSecurity]
public static extern Int32 NtGetCurrentProcessorNumber();
On Vista and higher:
[DllImport("kernel32"), SuppressUnmanagedCodeSecurity]
public static extern Int32 GetCurrentProcessorNumber();
Note that both return the current processor number the thread was executing
on at the moment of the call, and call here means the machine code
instruction.
Note that it's possible, under specific circumstances, that the dispatcher
switches a thread from one CPU to another during execution of that thread,
chances that this happens are very rare but not impossible. So you can't
assume that the remainder of the code is guaranteed to run on the same CPU
after the call to NtGetCurrentProcessorNumber returned.
Willy.