Blame view

OS.Spin/OS.Spin.Common/Machine/CpuTool.cs 1.54 KB
8ca6e89d   Tuo Wenbo   20211021
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  /********************************************************************************
  ** 类名称: CpuTool
  ** 描述   用于线程绑定CPU的处理类
  ** 作者   丁书杰
  ** 创建时间:2019/03/26
  ** 版权所有 (C) :中科视语(北京)科技有限公司
  *********************************************************************************/
  using System;
  using System.Runtime.InteropServices;
  
  namespace OS.Spin.Common.Machine
  {
      public class CpuTool
      {
          /// <summary>
          /// SetThreadAffinityMask 指定hThread 运行在 核心 dwThreadAffinityMask
          /// </summary>
          /// <param name="hThread">线程句柄</param>
          /// <param name="dwThreadAffinityMask">绑定的CPU标记</param>
          /// <returns></returns>
          [DllImport("kernel32.dll")]
          public static extern UIntPtr SetThreadAffinityMask(IntPtr hThread,
          UIntPtr dwThreadAffinityMask);
  
          /// <summary>
          /// 得到当前线程的handler
          /// </summary>
          /// <returns></returns>
          [DllImport("kernel32.dll")]
          public static extern IntPtr GetCurrentThread();
  
          /// <summary>
          /// 获取线程ID
          /// </summary>
          /// <param name="id">设置</param>
          /// <returns>返回的线程Id</returns>
         public static ulong SetCpuID(int id)
          {
              ulong cpuid = 0;
              if (id < 0 || id >= System.Environment.ProcessorCount)
              {
                  id = 0;
              }
              cpuid |= 1UL << id;
  
              return cpuid;
          }
      }
  }