我如何知道/查看线程在哪个核心上运行? (在win XP下)

发布于 2024-08-12 06:22:22 字数 903 浏览 3 评论 0原文

如果我有一个多线程程序,我如何知道在哪个核心上 每个线程运行?

C# 中的 win XP 还有其他解决方案吗?

我尝试这样做:

[DllImport("ntdll"), SuppressUnmanagedCodeSecurity]
public static extern int NtGetCurrentProcessorNumber();

并且我得到了这个异常:

System.EntryPointNotFoundException was unhandled
Message="Unable to find an entry point named 'NtGetCurrentProcessorNumber' in DLL 'ntdll'."
Source="XP_Multicore_try_0"
TypeName=""
StackTrace:
at XP_Multicore_try_0.Program.NtGetCurrentProcessorNumber()
at XP_Multicore_try_0.Program.loop() in C:\Documents and Settings\evyatarv\Desktop\XP_Multicore_try_0\XP_Multicore_try_0\Program.cs:line 24
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

If I have a multi-thread program, how can I know on which core
each thread run ?

Is there any another solution for win XP in C# ?

I try this:

[DllImport("ntdll"), SuppressUnmanagedCodeSecurity]
public static extern int NtGetCurrentProcessorNumber();

and I get this exception:

System.EntryPointNotFoundException was unhandled
Message="Unable to find an entry point named 'NtGetCurrentProcessorNumber' in DLL 'ntdll'."
Source="XP_Multicore_try_0"
TypeName=""
StackTrace:
at XP_Multicore_try_0.Program.NtGetCurrentProcessorNumber()
at XP_Multicore_try_0.Program.loop() in C:\Documents and Settings\evyatarv\Desktop\XP_Multicore_try_0\XP_Multicore_try_0\Program.cs:line 24
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

靖瑶 2024-08-19 06:22:22

首先你需要确定你想要什么“核心数”。每个核心都有多个与其关联的编号。在这种情况下,我假设您指的是核心的操作系统亲和性指数。

在这种情况下,假设是 x86 CPU,您可以获取核心的 APIC 编号(使用 CPUID 指令),然后将其用作亲和性索引等价表中的索引。

当然,您必须提前自己制作等效表。您可以通过访问每个核心(通过设置亲和力,从而了解亲和力指数)并检索其 APIC 来做到这一点。

请注意,我不知道如何在 C# 中做到这一点,我不是 C# 专家。但在 C 语言中相当容易,在汇编中甚至更容易。

First you need to identify what "core number" you want. Each core has several numbers associated with it. In this case I am assuming you mean the OS affinity index of the core.

In that case, assuming an x86 CPU, you can get the APIC number of the core (using the CPUID instruction), then use it as an index in a table of equivalence to the affinity index.

Of course you will have to make the table of equivalence yourself ahead of time. You could do that by going to each core (by setting affinity, thus knowing the affinity index) and retrieving its APIC.

Note that I don't know how you would do that in C#, I'm no C# expert. But it is fairly easy in C, and even easier in assembly.

傾旎 2024-08-19 06:22:22

我不确定有什么好的方法。虽然 Win32 提供了返回现有关联掩码的 SetThreadAffinityMask() 函数,但 .NET 线程与操作系统线程关联并不安全,如 这个问题

I'm not sure there is a good way. While Win32 offers the SetThreadAffinityMask() function which returns the existing affinity mask, the .NET thread is not safe to associate with an operating system thread as per this question.

謌踐踏愛綪 2024-08-19 06:22:22

根据 MSDN,NtGetCurrentProcessorNumber 可能会被更改或在未来版本的 Windows 中不可用。应用程序应使用 Kernel32 中的 GetCurrentProcessorNumber 函数。 dll 来代替。

GetCurrentProcessorNumber() 也并非在所有版本的 Windows 上都可用。它可在 Vista 及更高版本或 Windows Server 2003 及更高版本上使用。这意味着尝试在 XP 上使用它也会失败。

如果您要使用 API 调用,您至少应该先阅读文档。 :-)

话虽这么说,我不知道在 XP 及更早版本上有什么方法可以确定线程正在哪个核心或处理器上运行。这可能就是为什么他们从 Vista 开始将 GetProcessorNumber() 添加到 API 中。

According to MSDN, NtGetCurrentProcessorNumber may be altered or unavailable in future versions of Windows. Applications should use the GetCurrentProcessorNumber function from Kernel32.dll instead.

GetCurrentProcessorNumber() isn't available on all versions of Windows, either. It's available on Vista and above, or Windows Server 2003 and above. That means that trying to use it on XP will fail as well.

If you're going to use API calls, you should at least read the documentation first. :-)

That being said, I don't know of any way on XP and earlier to determine what core or processor a thread is being run on. That's probably why they added GetProcessorNumber() to the API starting with Vista.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文