4.10. Synchronize the Execution of Multiple Threads Using a Mutex

Problem

You need to coordinate the activities of multiple threads (possibly across process boundaries) to ensure the efficient use of shared resources or to ensure several threads are not updating the same shared resource at the same time.

Solution

Use the System.Threading.Mutex class.

How It Works

The Mutex has a similar purpose to the Monitor discussed in recipe 4-8—it provides a means to ensure only a single thread has access to a shared resource or section of code at any given time. However, unlike the Monitor, which is implemented fully within managed code, the Mutex is a wrapper around an operating system synchronization object. This means you can use a Mutex to synchronize ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.