Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. namespace Bisque
  6. {
  7.     public sealed class ProducerConsumer
  8.     {
  9.         const int MagicNumber = 10;                                 // Indicates how many times to bounce between ping and pong threads
  10.         private Object m_lock = new Object();                       // Lock to protect counter increment
  11.         private Queue<int> m_queue = new Queue<int>();
  12.  
  13.         // Ctor
  14.         public ProducerConsumer()
  15.         {
  16.         }
  17.  
  18.         // Ping
  19.         public void Producer()
  20.         {
  21.             int counter = 0;
  22.  
  23.             lock (m_lock)                                           // Allows only one thread at a time inside m_lock
  24.             {
  25.                 if(counter == 0)
  26.                 {
  27.                     Thread.Sleep(2000);                              // Get data chunks from some source
  28.                           // Wait if the thread is busy. 'wait' will hold this loop until something else pulses it to release the wait.
  29.                     Console.Write("producer {0}\t", counter);
  30.                     m_queue.Enqueue(counter);
  31.                     Monitor.Pulse(m_lock);                          // Releases consumer thread
  32.                     counter = 1;
  33.                 }
  34.                 while (counter < MagicNumber)
  35.                 {
  36.                     Thread.Sleep(2000);                              // Get data chunks from some source
  37.                     Monitor.Wait(m_lock);                           // Wait if the thread is busy. 'wait' will hold this loop until something else pulses it to release the wait.
  38.                     Console.Write("producer {0}\t", counter);
  39.                     m_queue.Enqueue(counter);
  40.                     Monitor.Pulse(m_lock);                          // Releases consumer thread
  41.  
  42.                     counter++;
  43.                 }
  44.             }
  45.         }
  46.  
  47.         public void Consumer()
  48.         {
  49.             lock (m_lock)                                           // Allows only one thread at a time inside m_lock
  50.             {
  51.                 Monitor.Pulse(m_lock);
  52.  
  53.                 while (Monitor.Wait(m_lock, 2500))                  // Wait in the loop while producer is busy. Exit when producer times-out. 1000 = 1 second; app will hang without this time-out value
  54.                 {
  55.                     int data = m_queue.Dequeue();
  56.                     Console.WriteLine("consumer {0}", data);
  57.                     Monitor.Pulse(m_lock);                          // Release consumer
  58.                 }
  59.             }
  60.         }
  61.     }
  62.  
  63.     class Program
  64.     {
  65.         static void Main(string[] args)
  66.         {
  67.             ProducerConsumer app = new ProducerConsumer();
  68.  
  69.             // Create 2 threads
  70.             Thread t_producer = new Thread(new ThreadStart(app.Producer));
  71.             Thread t_consumer = new Thread(new ThreadStart(app.Consumer));
  72.  
  73.             // Start threads
  74.             t_producer.Start();
  75.             t_consumer.Start();
  76.  
  77.             // Waith for the threads to complete
  78.             t_producer.Join();
  79.             t_consumer.Join();
  80.  
  81.             Console.WriteLine("\nPress any key to complete the program.\n");
  82.             Console.ReadKey(false);
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement