O'Reilly logo

.NET Framework 4.5 Expert Programming Cookbook by A.P. Rajshekhar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a shared resource

In a multi-threaded environment, resources are shared between multiple threads. So, it is critical that such resources should be capable of handling access by multiple threads. In this recipe, we will see how to create a shared resource that provides an in-built functionality that handles access by multiple threads using lock and monitors. .NET provides an implementation of monitor for threading in the form of Monitor class. In this recipe, we will see how to use the Monitor class to create a SharedBuffer class.

How to do it...

  1. Launch Visual Studio 2012. Create a new Console Application and name it ProducerConsumerModel.
  2. Add a new class to the project and name it SharedBuffer.
  3. Add a private variable of type int and name ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required