C# IntPtr 和 ProcessorAffinity - 如何超过 32 位?
C# IntPtr and ProcessorAffinity - how to exceed 32 bits?
我有一台超过 31 cpu 个内核的计算机。我想为第 31 个之后的 cpu 个核心分配一个进程。我的问题是 IntPtr 在这台机器上解析为 32 位整数。是64位机器运行 64位操作系统。这似乎违背了微软的声明 here
The IntPtr type is designed to be an integer whose size is
platform-specific. That is, an instance of this type is expected to be
32-bits on 32-bit hardware and operating systems, and 64-bits on
64-bit hardware and operating systems.
下面是我构建 IntPtr 的方式:
public static IntPtr GetCpuBinaryValue(int cpuIndex)
{
// The IntPtr type is designed to be an integer whose size is platform - specific.
// That is, an instance of this type is expected to be 32 - bits on 32 - bit hardware and operating systems,
// and 64 - bits on 64 - bit hardware and operating systems.
IntPtr ptr = new IntPtr(Convert.ToInt64(Math.Pow(2, cpuIndex)));
return ptr;
}
继续这个例子,这里是我设置 ProcessorAffinity 的方式:
using System.Diagnostics;
...
// set which processor to run the process on
process.ProcessorAffinity = GetCpuBinaryValue(coreIndex);
...
例如,我可以传入值“0”,它将 return 一个掩码为 00000000 00000000 00000000 00000001
的 IntPtr(32 位)
如果我传入“31”,它将 return 10000000 00000000 00000000 00000000
我希望将其转换为 64 位,例如,如果我传入值“32”,它将 return: 00000000 00000000 00000000 00000001 00000000 00000000 00000000 00000000
有办法解决这个问题吗?它固执地卡在 32 位上。
您可能 运行 在 x86
或 Any CPU
。 IntPtr
是platform-specific,但也要看进程本身。如果进程在64位模式下是运行,那么指针就是64位的,如果进程在32位模式下是运行,那么指针就是32位的。
如果您显式切换到 运行 作为 x64
(如 How to: Configure Projects to Target Platforms 中所述),则溢出异常消失,并且正确的值似乎存储在 return值。
我有一台超过 31 cpu 个内核的计算机。我想为第 31 个之后的 cpu 个核心分配一个进程。我的问题是 IntPtr 在这台机器上解析为 32 位整数。是64位机器运行 64位操作系统。这似乎违背了微软的声明 here
The IntPtr type is designed to be an integer whose size is platform-specific. That is, an instance of this type is expected to be 32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit hardware and operating systems.
下面是我构建 IntPtr 的方式:
public static IntPtr GetCpuBinaryValue(int cpuIndex)
{
// The IntPtr type is designed to be an integer whose size is platform - specific.
// That is, an instance of this type is expected to be 32 - bits on 32 - bit hardware and operating systems,
// and 64 - bits on 64 - bit hardware and operating systems.
IntPtr ptr = new IntPtr(Convert.ToInt64(Math.Pow(2, cpuIndex)));
return ptr;
}
继续这个例子,这里是我设置 ProcessorAffinity 的方式:
using System.Diagnostics;
...
// set which processor to run the process on
process.ProcessorAffinity = GetCpuBinaryValue(coreIndex);
...
例如,我可以传入值“0”,它将 return 一个掩码为 00000000 00000000 00000000 00000001
的 IntPtr(32 位)如果我传入“31”,它将 return 10000000 00000000 00000000 00000000
我希望将其转换为 64 位,例如,如果我传入值“32”,它将 return: 00000000 00000000 00000000 00000001 00000000 00000000 00000000 00000000
有办法解决这个问题吗?它固执地卡在 32 位上。
您可能 运行 在 x86
或 Any CPU
。 IntPtr
是platform-specific,但也要看进程本身。如果进程在64位模式下是运行,那么指针就是64位的,如果进程在32位模式下是运行,那么指针就是32位的。
如果您显式切换到 运行 作为 x64
(如 How to: Configure Projects to Target Platforms 中所述),则溢出异常消失,并且正确的值似乎存储在 return值。