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.
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
.
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...