internal class Program
{
[DllImport("kernel32.dll")]
private static extern uint GetTickCount(); //SetThreadAffinityMask 指定hThread 运行在 核心 dwThreadAffinityMask
[DllImport("kernel32.dll")]
private static extern UIntPtr SetThreadAffinityMask(IntPtr hThread,
UIntPtr dwThreadAffinityMask); //得到当前线程的handler
[DllImport("kernel32.dll")]
private static extern IntPtr GetCurrentThread(); private static void Main(string[] args)
{
var t1 = new Thread(sinaG);
Console.Write("Which core you will to use (Start from 0):");
string core = Console.ReadLine();
int coreNumber = ;
try
{
coreNumber = Int32.Parse(core);
}
catch
{
coreNumber = ;
}
t1.Start(coreNumber);
} private static void sinaG(object coreNumber)
{
int core = ;
try
{
core = (int) coreNumber;
}
catch
{
core = ;
}
SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(SetCpuID(core)));
//指定在核心1上运行
//SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(SetCpuID(0)));
//指定在核心2上运行
//SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(SetCpuID(1)));
//指定在核心3上运行
//SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(SetCpuID(2)));
//指定在核心4上运行
//SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(SetCpuID(3))) //split*count=2;也就是正弦函数的周期2 Pi,也就是把一个周期的细分为200份
double split = 0.01;
int count = ; double pi = 3.1415962525; //工作周期 300 ms
int interval = ; //每个工作周期里工作和空闲的毫秒数
var busySpan = new int[count];
var idealSpan = new int[count]; //根据正弦函数计算并填入每个工作周期的工作和空闲毫秒数
int half = interval/;
double radian = 0.0;
for (int i = ; i < count; i++)
{
busySpan[i] = (int) (half + Math.Sin(pi*radian)*half);
idealSpan[i] = interval - busySpan[i];
radian += split;
} uint startTime = ;
int j = ;
while (true)
{
j = j%count;
startTime = GetTickCount();
while ((GetTickCount() - startTime) <= busySpan[j])
{
;
}
Thread.Sleep(idealSpan[j]);
j++;
}
} //函数中的参数 dwThreadAffinityMask 为无符号长整型,用位标识那个核心
//比如:为简洁使用四位表示
//0x0001表示核心1,
//0x0010表示核心2,
//0x0100表示核心3,
//0x1000表示核心4
private static ulong SetCpuID(int id)
{
ulong cpuid = ;
if (id < || id >= Environment.ProcessorCount)
{
id = ;
}
cpuid |= 1UL << id;
return cpuid;
}
}

最新文章

  1. HTML基础篇之视频音频
  2. 227 Entering Passive Mode (xxx,xxx,,xxx,xxx,x)
  3. SQL组合查询的存储过程写法
  4. AsynTask用法
  5. JAVA学习之Ecplise IDE 使用技巧(2)第二章:键盘小快手,代码辅助
  6. 【转】Android 最火框架XUtils之注解机制详解
  7. UESTC_贪吃蛇 CDOJ 709
  8. LeeCode-Swap Nodes in Pairs
  9. HDU 2203 亲和串(KMP)
  10. mysql命令行导入导出数据库
  11. [工具开发] 分享两个基于Heapster 和 Influxdb 的 Grafana 监控仪表盘模板
  12. cf1073G Yet Another LCP Problem (SA+权值线段树)
  13. if-else练习
  14. python if 判断
  15. linux2.6.30.4内核移植(6)&mdash;&mdash;移植应用程序hello world常见的错误:-bin/sh ./hello not found
  16. AppBox中,如何在用户管理页面显示用户所属的多个角色?
  17. Swift语言 简明基础 代码演示样例
  18. 一些lua代码
  19. Java学习笔记——关于位运算符的问题
  20. Django项目运行时出现self.status.split(&#39; &#39;,1)[0], self.bytes_sent,ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接。

热门文章

  1. 计算两点间的距离-hdu2001
  2. 从setTimeout到浏览器线程机制
  3. Android Activity跳转动画,让你的APP瞬间绚丽起来
  4. 2014.06.20 (转)IEEE与论坛灌水
  5. MongoDB 分片操作
  6. codeforces gym 100463I Yawner
  7. cocos2dx lua 学习笔记(一)
  8. OC基础1:一些基本概念
  9. Cocos2d-x 3.1.1 Lua演示样例 ActionEaseTest(动作)
  10. Windows XPE 安装