Controlling Access to Data Objects with the Monitor Class


Controlling Access to Data Objects with the Monitor Class

The Monitor class provides another synchronization mechanism available in the .NET Compact Framework. The Monitor class is useful for protecting access to specific objects. You can use a Monitor to make sure that only one thread accesses a specific object at a time. This intent is subtly different from using a Mutex , which you can use to prevent two threads from executing a specific block of code at one time.

To use a Monitor to prevent multiple threads from accessing the same object, follow these steps:

  1. Acquire the object that you want to protect. When you want to access the protected object, first call System.Threading.Monitor.Enter() . Pass in the reference to the object to protect.

  2. Access the object. Keep the lock for as little time as possible, since other threads may be blocking, waiting for access to the same protected object.

  3. When you are finished with the protected object, call System.Threading.Monitor.Exit() . Pass in the reference to the object to stop protecting.

For example, assume that m_CustomerBankInfo is an object representing a customer's bank account information. You don't want two threads accessing it at the same time, since one thread might read the data while another is in the middle of writing to it. The code to access m_CustomerBankInfo safely looks like this:

 
 C# System.Threading.Monitor.Enter(m_CustomerBankInfo); // Access m_CustomerBankInfo // Now done with m_CustomerBankInfo System.Threading.Monitor.Exit(m_CustomerBankInfo); VB System.Threading.Monitor.Enter(m_CustomerBankInfo) ' Access m_CustomerBankInfo ' Now done with m_CustomerBankInfo System.Threading.Monitor.Exit(m_CustomerBankInfo) 

Simulating the Producer-Consumer Problem

The ProducerConsumer sample application demonstrates the use of Monitor objects to simulate a derivative of the classic Producer-Consumer computer science problem. The sample application uses two threads. One thread produces objects by incrementing the class member called m_objectsAvailable each time it wakes up. The consumer thread consumes an object each time it wakes up by decrementing m_objectsAvailable . The two threads use a Monitor to prevent both threads from trying to access the m_objectsAvailable member at the same time.

To make things interesting, the application starts with four objects available, but the consumer thread consumes objects slightly faster than the producer thread produces them. This effect is simulated by making the producer thread sleep longer than the consumer thread. As the program runs, you will notice that the number of objects slowly decreases until it reaches zero. Then each time a producer produces an object, the count will briefly reach one, before a consumer consumes the object.

This sample application is available in the directory \SampleApplications\Chapter4 . There are C# and Visual Basic versions.



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net