示例#1
0
        static int Main(string[] args)
        {
            // Check number of args
            if(args.Length != 2)
            {
                Console.WriteLine("USAGE:  CompareExchangeDouble " +
                    "/loops:<int> /addVal:<double>");
                return -1;
            }

            // Get the args
            int loops=100;
            double valueToAdd = 1E+100;
        
            for(int i=0;i<args.Length;i++)
            {
                if(args[i].ToLower().StartsWith("/loops:"))
                {
                    loops = Convert.ToInt32(args[i].Substring(7));
                    continue;
                }

                if(args[i].ToLower().StartsWith("/addval:"))
                {
					CultureInfo myCultureInfo = new CultureInfo("en-US");
					valueToAdd = Double.Parse(args[i].Substring(8), myCultureInfo);
                    continue;
                }
            }

            int rValue = 0;
            Thread[] threads = new Thread[100];
            ThreadSafe tsi = new ThreadSafe(loops,valueToAdd);
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(new ThreadStart(tsi.ThreadWorker));
                threads[i].Start();
            }
            
            tsi.Signal();

            for(int i=0;i<threads.Length;i++)
                threads[i].Join();
            double expected = 0.0D;
            for(int i=0;i<threads.Length*loops;i++)
                expected = (double)(expected + valueToAdd);
            if(tsi.Total == expected)
                rValue = 100;
            Console.WriteLine("Expected: "+expected);
            Console.WriteLine("Actual  : "+tsi.Total);
            Console.WriteLine("Test {0}", rValue == 100 ? "Passed" : "Failed");
            return rValue;
        }
        static int Main(string[] args)
        {
            // Check number of args
            if (args.Length != 2)
            {
                Console.WriteLine("USAGE:  CompareExchangeDouble " +
                                  "/loops:<int> /addVal:<double>");
                return(-1);
            }

            // Get the args
            int    loops      = 100;
            double valueToAdd = 1E+100;

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].ToLower().StartsWith("/loops:"))
                {
                    loops = Convert.ToInt32(args[i].Substring(7));
                    continue;
                }

                if (args[i].ToLower().StartsWith("/addval:"))
                {
                    CultureInfo myCultureInfo = new CultureInfo("en-US");
                    valueToAdd = Double.Parse(args[i].Substring(8), myCultureInfo);
                    continue;
                }
            }

            int rValue = 0;

            Thread[]   threads = new Thread[100];
            ThreadSafe tsi     = new ThreadSafe(loops, valueToAdd);

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(new ThreadStart(tsi.ThreadWorker));
                threads[i].Start();
            }

            tsi.Signal();

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }
            double expected = 0.0D;

            for (int i = 0; i < threads.Length * loops; i++)
            {
                expected = (double)(expected + valueToAdd);
            }
            if (tsi.Total == expected)
            {
                rValue = 100;
            }
            Console.WriteLine("Expected: " + expected);
            Console.WriteLine("Actual  : " + tsi.Total);
            Console.WriteLine("Test {0}", rValue == 100 ? "Passed" : "Failed");
            return(rValue);
        }