I feel a lot of pressure every day, in order to release the pressure, I have to learn a little more, today’s goal is to be a little stronger than I was yesterday! Let’s take a closer look at read-write locks, which I don’t use much in my code.

directory

  • Mutex: lock
  • Read and write locks: readwritelock
  • summary

Mutex: lock

Definition: A shared resource is mutually exclusive, allowing only one thread to enter. The first problem you encounter in multithreaded programming is concurrent access to shared resources. This problem occurs when two or more threads share access to an object and may simultaneously attempt to modify the object. When C# was first released, the lock statement implemented a basic way to ensure that only one thread could access a given resource (such as a data file), and it worked fine. The lock keyword in C# is easy to understand and turns the way we think about this problem on its head.

However, simple Lock has one major drawback: it does not distinguish between read-only and write access. A code example is as follows:

Object thisLock = new Object
Copy the code