Coordinating Threads with the Mutex ClassThe .NET Compact Framework includes the Mutex class, which is a useful entity for coordinating threads. The Mutex class's name is derived from the term mutual exclusion. The class creates a region of code that can be executed by only one thread at a time. A Mutex works by tracking which thread "holds" it. Only one thread may hold a Mutex at a time. If another thread tries to acquire it, then it blocks until the first thread releases its hold. To use a Mutex to force only one thread to execute code at a time, follow these steps:
The following sample code demonstrates the steps: C# // Create instance of Mutex class. The variable holding the // reference to this Mutex must be visible to the block of code // that will be protected. m_Mutex = new System.Threading.Mutex(false); // This is the code which we only want one thread at a time to execute // This call blocks until we acquire the Mutex m_Mutex.WaitOne(); // Put code here that only is executed by one thread at a time // Done, let others enter this code block now m_Mutex.ReleaseMutex(); VB ' Create instance of Mutex class. The variable holding the ' reference to this Mutex must be visible to the block of code ' that will be protected. m_Mutex = new System.Threading.Mutex(False) ' This is the code which we only want one thread at a time to execute ' This call blocks until we acquire the Mutex m_Mutex.WaitOne() ' Put code here that only is executed by one thread at a time ' Done, let others enter this code block now m_Mutex.ReleaseMutex() There are some special considerations for using the Mutex :
Suspending and Resuming Threads with MutexesThe desktop .NET Framework includes support for the Thread.Suspend and Thread.Resume methods, which can force a thread to sleep indefinitely and then wake it back up. The .NET Compact Framework does not support these methods , so building applications with threads that can be suspended and resumed requires some thought and architecting. One way to simulate the Thread.Suspend and Thread.Resume methods is by using the Mutex class. The technique is especially useful on code running in a loop. To support suspending a thread that is running code in a loop, follow these steps:
The following code, derived from the SuspendAndTerminateThreads sample application, demonstrates how to suspend and resume a thread running a loop: C# // This code runs in a loop. The thread can be suspended by calling // m_Mutex.WaitOne() while (!l_quit) { m_Mutex.WaitOne(); // Insert useful code here m_Mutex.ReleaseMutex(); } // This code runs on another thread, and will suspend the code above // This call blocks until the other thread finishes its loop // iteration, then that thread blocks m_Mutex.WaitOne(); // Other thread is now blocked ... // And now resume the other thread m_Mutex.ReleaseMutex(); VB ' This code runs in a loop. The thread can be suspended by calling ' m_Mutex.WaitOne() while (l_quit = False) m_Mutex.WaitOne() ' Insert useful code here m_Mutex.ReleaseMutex() End While ' This code runs on another thread, and will suspend the code above ' This call blocks until the other thread finishes its loop ' iteration, then that thread blocks m_Mutex.WaitOne() ' Other thread is now blocked ... ' And now resume the other thread m_Mutex.ReleaseMutex() Examining a Sample Application: SuspendAndTerminateThreadsThis sample application has C# and VB versions in the folder \SampleApplications\Chapter4 . It demonstrates how to suspend and resume threads by using a Mutex and also how to set up a thread so that it can check to see if it should terminate. The application has a button at the bottom labeled Start Threads. Clicking this button causes two threads to launch, one that counts by ones, and another by tens. Either of these threads can be independently suspended, resumed, or terminated by clicking the appropriately labeled button. |