Getting the CPU number of a given thread in C #

In C #, is there any method for getting the cpu identifier (or main number) of the kernel executing the thread? I have a quad-core processor, and I would like to know that when distributing some threads, which kernels do they allocate?

I found that System.Environment.ProcessorCount gives the total number of processors present, but is there any way to find out which particular cpu instance this thread is running.

+4
source share
1 answer

I do not know about the native .NET function that can provide this. But if you are ready to use P / Invoke and are running Windows 2003, Vista or later, you can call GetCurrentProcessorNumber .

Of course, this will give you only the processor (kernel) that runs the thread on this particular time fragment, and the next time the thread is scheduled, it can run on a completely different core, even if the OS tends to schedule the thread on the same core for reasons of performance.

+5
source

All Articles