Reputation: 1233
In C#, is there any method to get the cpu id (or core number) of the core that is executing a thread? I have a quad core processor and I wanted to know that on spanning some threads, which cores do they get alloted?
I found out that System.Environment.ProcessorCount gives the total number of cpus present, but is there any way to know which specific cpu instance is executing a given thread.
Upvotes: 4
Views: 1356
Reputation: 53699
I do not know of a native .NET function that can provide this. But if you are prepared to use P/Invoke and are running on Windows 2003, Vista or later then you can call GetCurrentProcessorNumber.
Of course this will only give you the CPU (core) that is executing the thread at that particular time slice and the next time the thread is scheduled it might be running on a completely different core even though the OS will strive to schedule a thread on the same core for performance reasons.
Upvotes: 5