4.8. Synchronize the Execution of Multiple Threads Using a Monitor

Problem

You need to coordinate the activities of multiple threads 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

Identify an appropriate object to use as a mechanism to control access to the shared resource/data. Use the Shared method Monitor.Enter to acquire a lock on the object, and use the Shared method Monitor.Exit to release the lock so another thread may acquire it.

How It Works

The greatest challenge in writing a multithreaded application is ensuring that the threads work in concert. This is commonly referred to as thread synchronization and includes the following:

  • Ensuring ...

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.