Monitor Synchronization Technology Sample

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

This sample demonstrates how to use the Monitor class for thread synchronization. The Monitor type's static functions are used to enforce mutually exclusive access to a protected block of code.

Review comments in the source code and build.proj files for more information on thread synchronization and the Monitor class.

For more information about using the samples, see the following topics:

To build the sample using the command prompt

  1. Open a Command Prompt window and navigate to one of the language-specific subdirectories for the sample.

  2. Type msbuild MonitorSynchronizationCS.sln or msbuild MonitorSynchronizationVB.sln, depending on your choice of programming language, at the command line.

To build the sample using Visual Studio

  1. Open Windows Explorer and navigate to one of the language-specific subdirectories for the sample.

  2. Double-click the icon for MonitorSynchronizationCS.sln or MonitorSynchronizationVB.sln, depending on your choice of programming language, to open the file in Visual Studio.

  3. On the Build menu, click Build Solution.

To run the sample

  1. Navigate to the directory that contains the new executable, using the command prompt.

  2. Type Monitor.exe from the command line.

Note

This sample builds a console application. You must launch it using the command prompt in order to view its output. When running the sample, notice that the "Start Writing" and "Stop Writing" output for each writer occurs successively without overlap from threads with different IDs. This is because the threads hold exclusive access to the resource.

Remarks

The following bullets briefly describe the classes and technologies used by this sample.

  • Thread Synchronization

    • Monitor Provides static functions for entering and exiting a protected block of code. You cannot instantiate an instance of the Monitor type. Instead, you pass to its static methods a reference to any object-derived type that you wish to use as a single node of synchronization.

      Note

      Both Visual Basic and C# implement special keywords to take advantage of the Monitor type's static methods. C# uses the lock keyword, and Visual Basic uses the SyncLock keyword.

    • AutoResetEvent The sample's main thread waits on an AutoResetEvent object until it is set, which indicates that the last of the asynchronous functions are finished doing their work.

    • Interlocked The sample uses the static Decrement method to asynchronously access a counter variable to indicate when the AutoResetEvent should be set.

  • Threading

    • ThreadPool When writing managed code, it is suggested that whenever possible, developers use the QueueUserWorkItem method to implement asynchronous method calls. The sample uses this approach to execute code that contends for a logical resource.
  • Delegates

See Also

Reference

AutoResetEvent class

Delegate class

Interlocked class

Monitor class

System.Threading namespace

Thread class

ThreadPool class

ThreadStart delegate

WaitCallback delegate

Concepts

The Managed Thread Pool

Other Resources

Managed Threading

Threading Objects and Features