windows 识别特定线程使用的处理器(核心)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3366932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Identify processor (core) is used by specific thread
提问by DenisL
I would like to know if it is possible to identify physical processor (core) used by thread with specific thread-id
?
我想知道是否可以通过特定的thread-id
?
For example, I have a multithreaded application that has two (2) threads (thread-id = 10
and thread-id = 20
, for instance). I run the application on a system that has a dual core processor (core 1 and core 2). So, how do I to get core number used by thread with thread-id = 20
?
例如,我有一个具有两(2)的螺纹(多线程应用程序thread-id = 10
和thread-id = 20
,例如)。我在具有双核处理器(核心 1 和核心 2)的系统上运行该应用程序。那么,我如何获得线程使用的核心数thread-id = 20
?
P.S. Windows platforms.
PS Windows 平台。
Thank you,
谢谢,
Denis.
丹尼斯。
回答by Patrick
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).
如果您使用线程关联,您可以查看 Windows 线程关联函数 ( http://msdn.microsoft.com/en-us/library/ms684847%28v=VS.85%29.aspx)。
回答by Roland Pihlakas
There are functions called GetCurrentProcessorNumber
(available since Server 2003 and Vista) and GetCurrentProcessorNumberEx
(available since Server 2008 R2 and Windows 7).
有调用的函数GetCurrentProcessorNumber
(自 Server 2003 和 VistaGetCurrentProcessorNumberEx
起可用)和(自 Server 2008 R2 和 Windows 7 起可用)。
See also this question's answersfor more related options and considerations (including Windows XP - primarily this answer describing the use of cpuid instruction).
另请参阅此问题的答案以获取更多相关选项和注意事项(包括 Windows XP - 主要是此答案描述了 cpuid 指令的使用)。
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.
当然,调度程序可以随时更改内核编号,因此,如果您需要确定,那么如果您在测量或执行短时间的某事之前和之后检查内核编号,可能有助于合理的数量,如果内核编号仍然相同,那么您就知道中间代码最有可能在哪个内核上执行。