프로그램의 실행 중인 CPU 코어를 지정합니다.
9849 단어 cpu
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 = 0;
try
{
coreNumber = Int32.Parse(core);
}
catch
{
coreNumber = 0;
}
t1.Start(coreNumber);
}
private static void sinaG(object coreNumber)
{
int core = 0;
try
{
core = (int) coreNumber;
}
catch
{
core = 0;
}
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 = 200;
double pi = 3.1415962525;
// 300 ms
int interval = 300;
//
var busySpan = new int[count];
var idealSpan = new int[count];
//
int half = interval/2;
double radian = 0.0;
for (int i = 0; i < count; i++)
{
busySpan[i] = (int) (half + Math.Sin(pi*radian)*half);
idealSpan[i] = interval - busySpan[i];
radian += split;
}
uint startTime = 0;
int j = 0;
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 = 0;
if (id < 0 || id >= Environment.ProcessorCount)
{
id = 0;
}
cpuid |= 1UL << id;
return cpuid;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
CPU와 GPU의 차이 및 장치 확인(Ubuntu 기준)CPU는 컴퓨터의 두뇌를 담당한다. 다양한 환경에서의 작업을 빠르게 수행하기 위해 ALU()의 구조가 복잡하고 명령어 하나로 처리할 수 없는 기능도 많으며 각종 제어 처리를 위한 부분이 많다. 반면, GPU는 특화된...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.