C# Source Code: Using the Monitor class to synchronise access to an object
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Using the Monitor class to synchronise access to an object
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, June 03, 2004
Hits:
803
Category:
Threading/Asynchronous operations
Article:
The code below demonstrates how to use a Monitor class to synchronizes access to an Queue object between two threads. The first thread adds items on the queue then uses the Pulse method followed by a Wait, to wake the second thread. The second thread then removes the items and also uses a Pulse followed by a Wait to restart the first thread. using System; using System.Threading; using System.Collections; namespace VBusers.Threading { ///
/// An example class containing two methods that are run on different threads /// to add and remove items from a Queue. The monitor class is use to add and remove /// locks on the Queue class. ///
class QueueSample { const int MAX_QUEUE_LENGTH = 10; Queue _queueItems; public QueueSample() { _queueItems = new Queue(); } ///
/// Thread adds items to a queue ///
public void FirstThread() { int counter = 0; Console.WriteLine("{0} FirstThread.BeforeLock.",DateTime.Now); lock(_queueItems) { Console.WriteLine("{0} FirstThread.AfterLock. Item " + counter.ToString(),DateTime.Now); while(counter < MAX_QUEUE_LENGTH) { //Wait, if the queue is busy. Console.WriteLine("{0} FirstThread.BeforeWait.",DateTime.Now); Monitor.Wait(_queueItems); Console.WriteLine("{0} FirstThread.AfterWait.",DateTime.Now); //Push one element. _queueItems.Enqueue(counter); //Release the waiting thread. Console.WriteLine("{0} FirstThread.BeforePulse. Added " + counter.ToString() + ".",DateTime.Now); Monitor.Pulse(_queueItems); Console.WriteLine("{0} FirstThread.AfterPulse.",DateTime.Now); counter++; } Console.WriteLine("{0} FirstThread.AfterLoop. ",DateTime.Now); } Console.WriteLine("{0} FirstThread.AfterLock. ",DateTime.Now); } ///
/// Thread removes items from a queue ///
public void SecondThread() { Console.WriteLine("{0} SecondThread.BeforeLock.", DateTime.Now); lock(_queueItems) { Console.WriteLine("{0} SecondThread.AfterLock. ", DateTime.Now); //Release the waiting thread. Console.WriteLine("{0} SecondThread.BeforePulse. ", DateTime.Now); Monitor.Pulse(_queueItems); Console.WriteLine("{0} SecondThread.AfterPulse. ", DateTime.Now); //Wait in the loop, while the queue is busy. //Exit on the time-out when the first thread stops. Console.WriteLine("{0} SecondThread.BeforeWait.", DateTime.Now); while(Monitor.Wait(_queueItems,500)) { Console.WriteLine("{0} SecondThread.AfterWait.", DateTime.Now); //Pop the first element in the queue int counter = (int)_queueItems.Dequeue(); //Release the waiting thread. Console.WriteLine("{0} SecondThread.BeforePulse. Removed " + counter.ToString() + ".", DateTime.Now); Monitor.Pulse(_queueItems); Console.WriteLine("{0} SecondThread.AfterPulse.", DateTime.Now); } Console.WriteLine("{0} SecondThread.AfterLoop.", DateTime.Now); } Console.WriteLine("{0} SecondThread.AfterLock.", DateTime.Now); } //Return the number of queue elements. public int GetQueueCount() { return _queueItems.Count; } } ///
/// Demonstration code showing two theads adding and removing items from /// a queue using the Monitor object to control the lock synchronisation. ///
/// public class Demo { static void Main(string[] args) { //Create the QueueSample object. QueueSample queueExample = new QueueSample(); //Create the first thread. Thread tFirst = new Thread(new ThreadStart(queueExample.FirstThread)); //Create the second thread. Thread tSecond = new Thread(new ThreadStart(queueExample.SecondThread)); //Start first thread tFirst.Start(); //Allow enough time for the first thread to startup and grab the lock Thread.Sleep(10); //Start the second thread tSecond.Start(); //wait to the end of the two threads tFirst.Join(); tSecond.Join(); //Print the number of queue elements. Console.WriteLine("Queue Count = " + queueExample.GetQueueCount().ToString()); Console.ReadLine(); } } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet