/********************************************************************************
** 类名称: CpuTool
** 描述 : 用于线程绑定CPU的处理类
** 作者 : 丁书杰
** 创建时间:2019/03/26
** 版权所有 (C) :中科视语(北京)科技有限公司
*********************************************************************************/
using System;
using System.Runtime.InteropServices;
namespace OS.Spin.Common.Machine
{
public class CpuTool
{
///
/// SetThreadAffinityMask 指定hThread 运行在 核心 dwThreadAffinityMask
///
/// 线程句柄
/// 绑定的CPU标记
///
[DllImport("kernel32.dll")]
public static extern UIntPtr SetThreadAffinityMask(IntPtr hThread,
UIntPtr dwThreadAffinityMask);
///
/// 得到当前线程的handler
///
///
[DllImport("kernel32.dll")]
public static extern IntPtr GetCurrentThread();
///
/// 获取线程ID
///
/// 设置
/// 返回的线程Id
public static ulong SetCpuID(int id)
{
ulong cpuid = 0;
if (id < 0 || id >= System.Environment.ProcessorCount)
{
id = 0;
}
cpuid |= 1UL << id;
return cpuid;
}
}
}