我有一台超过31个cpu内核的电脑。我想为31号以后的cpu核心分配一个进程。我的问题是IntPtr在这台机器上解析为32位整数。它是一个运行64位操作系统的64位机器。这似乎违背什么是微软hereC#IntPtr和ProcessorAffinity - 如何超过32位?
的IntPtr的类型被设计为一个整数,其大小为 平台的具体说明。也就是说,这种类型的实例预计在32位硬件和操作系统上为32位,在64位硬件和操作系统上为64位。
这里是我正在构建的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;
}
,并继续例子,这里是我设置的处理器亲和性:
using System.Diagnostics;
...
// set which processor to run the process on
process.ProcessorAffinity = GetCpuBinaryValue(coreIndex);
...
例如,我可以通过在值为“0”,它将返回一个带有00000000掩码的IntPtr(32位)0000000000000000000000000001
如果我传入“31”,它将会返回E打开千万00000000 00000000 00000000
我的愿望是,它强制转换为64位,例如,如果我通过在值“32”,则回复:00000000 00000000 00000000 00000001 00000000 00000000 00000000 00000000
有一种解决这个问题的方法?它固执地停留在32位。
你试图通过这样做解决什么问题? –
这是底层winapi函数SetProcessAffinityMask()中的一个限制。在32位模式下,它只能处理32个处理器。如果你不能碰到AnyCPU,你可以做什么,你的硬件比你的软件强大得多。 –