The theoretical question. If we need to synchronize threads, then in most cases probably lock enough, it is almost the same as the Monitor class, and what's the difference with the Mutex class, is it really that the Monitor is a static thing, and for Mutex you need to create an instance of the class.

  • In short, Mutex is a feature of the OS, methods from the static class Monitor are a feature of .NET. - Zergatul

1 answer 1

The Monitor class works only within one process. Mutex 'for the same, you can specify an identifier that will be unique for the entire OS.

For example, this is useful if you have two different applications running, but there should be synchronization between them (for example, writing to a file and reading).

Just look at this question.

PS regarding most cases and using lock - if you have tasks waiting ( await ) - then lock will not work .

  • About synchronization and async/await - NitoAsync to help. - andreycha
  • @andreycha, I used the library, but it didn’t seem to have a release version for netstandard2.0 - A1essandro
  • @andreycha and what is the advantage of this? I just used SemaphoreSlim for asynchronous lock - tym32167
  • @ A1essandro c .NET Standard / .NET Core the following story - they split the library into several different packages. See here for details. - andreycha
  • @ tym32167 abstraction. For example, the same AsyncLock more convenient to use with using than the semaphore with try/finally . Well, there are still a lot of useful classes (for example, AsyncLazy ), which, in fact, are just convenient wrappers. - andreycha