Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Resources;
- using ThreadingTest.Properties;
- using System.Drawing;
- using System.Diagnostics;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ThreadingTest
- {
- class Program
- {
- const int loopSize = 1;
- static Bitmap[] Images = null;
- const int ImagesCount = 4;
- static int counter = 0;
- static object synchObj = null;
- static bool finished;
- static void Main(string[] args)
- {
- Console.WriteLine("Enter S for serial P for Parallel T for TPL L for ThreadingPool");
- string input = Console.ReadLine().ToLower();
- LoadImages();
- Stopwatch sw = Stopwatch.StartNew();
- if (input == "s")
- {
- for (int i = 0; i < loopSize; i++)
- {
- SerialRead();
- }
- }
- else if (input == "p")
- {
- for (int i = 0; i < loopSize; i++)
- {
- ParallelRead();
- }
- }
- else if (input == "l")
- {
- for (int i = 0; i < loopSize; i++)
- {
- ThreadPoolRead();
- }
- }
- else if (input == "t")
- {
- for (int i = 0; i < loopSize; i++)
- {
- TPLRead();
- }
- }
- sw.Stop();
- Console.WriteLine("Average Time taken {0} milliseconds", sw.ElapsedMilliseconds / loopSize);
- }
- private static void TPLRead()
- {
- Parallel.For(1, 5,
- delegate(int index)
- {
- Bitmap b = Images[index - 1];
- for (int x = 0; x < b.Width; x++)
- {
- for (int y = 0; y < b.Height; y++)
- {
- b.GetPixel(x, y);
- }
- }
- }
- );
- }
- private static void ThreadPoolRead()
- {
- synchObj = new object();
- finished = false;
- counter = 0;
- for (int i = 0; i < ImagesCount; i++)
- {
- ThreadPool.UnsafeQueueUserWorkItem(ThreadRead, i + 1);
- }
- if (!finished)
- lock (synchObj)
- Monitor.Wait(synchObj);
- }
- private static void LoadImages()
- {
- Images = new Bitmap[ImagesCount];
- for (int i = 1; i <= ImagesCount; i++)
- {
- Images[i-1]= (Bitmap)Resources.ResourceManager.GetObject("_" + i);
- }
- }
- static void SerialRead()
- {
- for (int i = 1; i <=ImagesCount ; i++)
- {
- Bitmap b = Images[i - 1];
- for (int x = 0; x < b.Width; x++)
- {
- for (int y = 0; y < b.Height; y++)
- {
- b.GetPixel(x, y);
- }
- }
- }
- }
- static void ParallelRead()
- {
- synchObj = new object();
- finished = false;
- counter = 0;
- Thread[] th = new Thread[ImagesCount];
- //int processorCount = Environment.ProcessorCount;
- for (int i = 1; i <= ImagesCount; i++)
- {
- th[i-1] = new Thread(new ParameterizedThreadStart(ThreadRead));
- th[i-1].Start(i);
- }
- if(!finished)
- lock (synchObj)
- Monitor.Wait(synchObj);
- GC.KeepAlive(th);
- }
- static void ThreadRead(object param)
- {
- int index = (int)param;
- Bitmap b = Images[index - 1];
- for (int x = 0; x < b.Width; x++)
- {
- for (int y = 0; y < b.Height; y++)
- {
- b.GetPixel(x, y);
- }
- }
- Interlocked.Increment(ref counter);
- if (counter == ImagesCount)
- {
- finished = true;
- lock (synchObj)
- Monitor.Pulse(synchObj);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement