set thread affioph0iinity cpu

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;
using System.IO;
using System.Xml;
using System.Configuration;
using System.Data;
using System.Net;

using System.IO.Compression;
using System.Reflection;

using System.Runtime.InteropServices;


namespace abc
{
    class Program
    {
        static int numberOfThreads = 1;
        static int lastProcessor = 0;

        [DllImport("kernel32.dll")]
        private static extern IntPtr GetCurrentThread();
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern UIntPtr SetThreadAffinityMask(IntPtr handle, UIntPtr mask);
        
        static void Main(string[] args)
        {
            int concurrentThread = 1;
            int.TryParse(ConfigurationManager.AppSettings["concurrentThread"], out concurrentThread);
            if (concurrentThread < 1)
                concurrentThread = 1;
            if (concurrentThread > 99)
                concurrentThread = 99;
            Console.ReadLine();
            Console.WriteLine("Press enter to exit...");
            Console.WriteLine("start processing xml files...");

                       
            int.TryParse(ConfigurationManager.AppSettings["LastProcessor"], out lastProcessor); 

            numberOfThreads = concurrentThread;
                         
            for (int i = 0; i < concurrentThread; i++)
            {
                 Thread workThread = new Thread(new ParameterizedThreadStart(ProcessXMLFiles));
                workThread.Start(i);
            }
            
            Console.ReadLine();
            Console.ReadLine();
        }


        static void ProcessXMLFiles(Object stateInfo)
        {
            int threadNum = (int)stateInfo;
            ulong procMask = (ulong)System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity;
            ulong mask = (ulong) lastProcessor >>threadNum;
            UIntPtr lastAff = SetThreadAffinityMask(GetCurrentThread(), (UIntPtr)mask);
            Console.WriteLine("set thread affinity = {0}", (ulong)lastAff);
                
            string threadKey = "T" + threadNum.ToString().PadLeft(2, '0');            
            DateTime timeStart = DateTime.Now;
            string xmlString = "<Events>" +  "</Events>";

            XmlDocument eventXmlDoc = new XmlDocument();
            for(int j = 0; j < 900; j++)
            {
                Console.WriteLine("{0}: processing #{1}", threadKey, j.ToString());

                for (int i = 0; i < 1000; i++)
                {
                    eventXmlDoc.LoadXml(xmlString);
                }              
            }
            //========== for timing ======================
            DateTime timeEnd = DateTime.Now;
            Console.WriteLine("XML process time = {0} seconds, [{1}, {2}]", (int)timeEnd.Subtract(timeStart).TotalSeconds, timeStart, timeEnd);
            //========== end =============================

            //security2Writer.Close();
            Console.WriteLine("end of processing xml files...");
        }


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值