프로그램의 실행 중인 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;

        }

    }

좋은 웹페이지 즐겨찾기