Identify processor (core) is used by specific thread

前端 未结 2 1954
天涯浪人
天涯浪人 2020-12-19 07:48

I would like to know if it is possible to identify physical processor (core) used by thread with specific thread-id?

For example, I have a multithreade

相关标签:
2条回答
  • 2020-12-19 08:14

    Unless you use thread-affinity, threads are not assigned to specific cores. With every time slice, the thread can be executed on different cores. This means that if there would be a function to get the core of a thread, by the time you get the return value, there's a big chance that the thread is already executing on another core.

    If you are using thread-affinity, you could take a look at the Windows thread-affinity functions (http://msdn.microsoft.com/en-us/library/ms684847%28v=VS.85%29.aspx).

    0 讨论(0)
  • 2020-12-19 08:24

    There are functions called GetCurrentProcessorNumber (available since Server 2003 and Vista) and GetCurrentProcessorNumberEx (available since Server 2008 R2 and Windows 7).

    See also this question's answers for more related options and considerations (including Windows XP - primarily this answer describing the use of cpuid instruction).

    Of course the core number can be changed any time by the scheduler so if You need to be sure then perhaps it helps for a reasonable amount if You check the core number both before and after something You measured or executed for a short amount of time, and if the core number is still same then You know on which core most likely the intermediate code also executed.

    0 讨论(0)
提交回复
热议问题