MultiThreading and Dual Core

  • Thread starter Thread starter arunairs
  • Start date Start date
A

arunairs

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 PInvoke some Win32 functions to get that information; but why would
you want to?
 
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?

No, because it can change at any time, including during the call to the
function that would find out.
 
You can PInvoke some Win32 functions to get that information; but why would
you want to?

--
Browsehttp://connect.microsoft.com/VisualStudio/feedback/and vote.http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#





- Show quoted text -

I am comparing the performance of my code ( multi threaded, cpu
intensive) in a single core and dual-core systems and only see a
performance gain of around 15%. This led me to wonder if the runtime
was actually using the dual core. Hence I was wondering if there was
any way I could find this out.

Alternatively, is there any way to get the usage statistics of both
the cores within my program?

thanks,

Arun
 
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.
 
Back
Top