Using the ReaderWriterLockSlim construct
This recipe will describe how to create a thread-safe mechanism to read and write to a collection from multiple threads using a ReaderWriterLockSlim
construct. ReaderWriterLockSlim
represents a lock that is used to manage access to a resource, allowing multiple threads for reading or exclusive access for writing.
Getting ready
To step through this recipe, you will need Visual Studio 2015. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter2\Recipe8
.
How to do it...
To understand how to create a thread-safe mechanism to read and write to a collection from multiple threads using the ReaderWriterLockSlim
construct, perform the following steps:
Start Visual Studio 2015. Create a new C# console application project.
In the
Program.cs
file, add the followingusing
directives:using System; using System.Collections.Generic; using System.Threading; using static System.Console; using static System.Threading.Thread...