Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace Bisque
- {
- public sealed class ProducerConsumer
- {
- const int MagicNumber = 10; // Indicates how many times to bounce between ping and pong threads
- private Object m_lock = new Object(); // Lock to protect counter increment
- private Queue<int> m_queue = new Queue<int>();
- // Ctor
- public ProducerConsumer()
- {
- }
- // Ping
- public void Producer()
- {
- int counter = 0;
- lock (m_lock) // Allows only one thread at a time inside m_lock
- {
- if(counter == 0)
- {
- Thread.Sleep(2000); // Get data chunks from some source
- // Wait if the thread is busy. 'wait' will hold this loop until something else pulses it to release the wait.
- Console.Write("producer {0}\t", counter);
- m_queue.Enqueue(counter);
- Monitor.Pulse(m_lock); // Releases consumer thread
- counter = 1;
- }
- while (counter < MagicNumber)
- {
- Thread.Sleep(2000); // Get data chunks from some source
- Monitor.Wait(m_lock); // Wait if the thread is busy. 'wait' will hold this loop until something else pulses it to release the wait.
- Console.Write("producer {0}\t", counter);
- m_queue.Enqueue(counter);
- Monitor.Pulse(m_lock); // Releases consumer thread
- counter++;
- }
- }
- }
- public void Consumer()
- {
- lock (m_lock) // Allows only one thread at a time inside m_lock
- {
- Monitor.Pulse(m_lock);
- 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
- {
- int data = m_queue.Dequeue();
- Console.WriteLine("consumer {0}", data);
- Monitor.Pulse(m_lock); // Release consumer
- }
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- ProducerConsumer app = new ProducerConsumer();
- // Create 2 threads
- Thread t_producer = new Thread(new ThreadStart(app.Producer));
- Thread t_consumer = new Thread(new ThreadStart(app.Consumer));
- // Start threads
- t_producer.Start();
- t_consumer.Start();
- // Waith for the threads to complete
- t_producer.Join();
- t_consumer.Join();
- Console.WriteLine("\nPress any key to complete the program.\n");
- Console.ReadKey(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement