Advertisement
Guest User

Threading Experiment

a guest
May 19th, 2010
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Resources;
  6. using ThreadingTest.Properties;
  7. using System.Drawing;
  8. using System.Diagnostics;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11.  
  12. namespace ThreadingTest
  13. {
  14.     class Program
  15.     {
  16.         const int loopSize = 1;
  17.         static Bitmap[] Images = null;
  18.         const int ImagesCount = 4;
  19.         static int counter = 0;
  20.         static object synchObj = null;
  21.         static bool finished;
  22.         static void Main(string[] args)
  23.         {
  24.             Console.WriteLine("Enter S for serial P for Parallel T for TPL L for ThreadingPool");
  25.             string input = Console.ReadLine().ToLower();
  26.             LoadImages();
  27.             Stopwatch sw = Stopwatch.StartNew();
  28.             if (input == "s")
  29.             {
  30.                 for (int i = 0; i < loopSize; i++)
  31.                 {
  32.                     SerialRead();
  33.                 }
  34.                
  35.             }
  36.             else if (input == "p")
  37.             {
  38.                 for (int i = 0; i < loopSize; i++)
  39.                 {
  40.                     ParallelRead();
  41.                 }
  42.                
  43.             }
  44.             else if (input == "l")
  45.             {
  46.                 for (int i = 0; i < loopSize; i++)
  47.                 {
  48.                     ThreadPoolRead();
  49.                 }
  50.  
  51.             }
  52.             else if (input == "t")
  53.             {
  54.                 for (int i = 0; i < loopSize; i++)
  55.                 {
  56.                     TPLRead();
  57.                 }
  58.  
  59.             }
  60.             sw.Stop();
  61.             Console.WriteLine("Average Time taken {0} milliseconds", sw.ElapsedMilliseconds / loopSize);
  62.         }
  63.  
  64.         private static void TPLRead()
  65.         {
  66.             Parallel.For(1, 5,
  67.                 delegate(int index)
  68.                 {  
  69.                     Bitmap b = Images[index - 1];
  70.                     for (int x = 0; x < b.Width; x++)
  71.                     {
  72.                         for (int y = 0; y < b.Height; y++)
  73.                         {
  74.                             b.GetPixel(x, y);
  75.                         }
  76.                     }
  77.                 }
  78.             );
  79.         }
  80.  
  81.         private static void ThreadPoolRead()
  82.         {
  83.             synchObj = new object();
  84.             finished = false;
  85.             counter = 0;
  86.             for (int i = 0; i < ImagesCount; i++)
  87.             {
  88.                 ThreadPool.UnsafeQueueUserWorkItem(ThreadRead, i + 1);
  89.             }
  90.             if (!finished)
  91.                 lock (synchObj)
  92.                     Monitor.Wait(synchObj);
  93.         }
  94.  
  95.         private static void LoadImages()
  96.         {
  97.             Images = new Bitmap[ImagesCount];
  98.             for (int i = 1; i <= ImagesCount; i++)
  99.             {
  100.                 Images[i-1]= (Bitmap)Resources.ResourceManager.GetObject("_" + i);
  101.             }
  102.            
  103.         }
  104.         static void SerialRead()
  105.         {
  106.             for (int i = 1; i <=ImagesCount ; i++)
  107.             {
  108.                 Bitmap b = Images[i - 1];
  109.                 for (int x = 0; x < b.Width; x++)
  110.                 {
  111.                     for (int y = 0; y < b.Height; y++)
  112.                     {
  113.                         b.GetPixel(x, y);
  114.                     }
  115.                 }
  116.             }
  117.            
  118.         }
  119.         static void ParallelRead()
  120.         {
  121.             synchObj = new object();
  122.             finished = false;
  123.             counter = 0;
  124.             Thread[] th = new Thread[ImagesCount];
  125.             //int processorCount = Environment.ProcessorCount;
  126.             for (int i = 1; i <= ImagesCount; i++)
  127.             {
  128.                 th[i-1] = new Thread(new ParameterizedThreadStart(ThreadRead));
  129.                 th[i-1].Start(i);
  130.                
  131.             }
  132.             if(!finished)
  133.             lock (synchObj)
  134.                 Monitor.Wait(synchObj);
  135.            
  136.             GC.KeepAlive(th);
  137.            
  138.         }
  139.         static void ThreadRead(object param)
  140.         {
  141.             int index = (int)param;
  142.             Bitmap b = Images[index - 1];
  143.             for (int x = 0; x < b.Width; x++)
  144.             {
  145.                 for (int y = 0; y < b.Height; y++)
  146.                 {
  147.                     b.GetPixel(x, y);
  148.                 }
  149.             }
  150.             Interlocked.Increment(ref counter);
  151.             if (counter == ImagesCount)
  152.             {
  153.                 finished = true;
  154.                 lock (synchObj)
  155.                     Monitor.Pulse(synchObj);
  156.  
  157.             }
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement