Hello, I looked through such concepts as Mutex, Semaphore, SemaphoreSlim, AutoResentEvent, etc. During the proceedings with them such questions arose:

1) If you don’t give Mutex a name, it turns out that it starts working like a normal lock, does synchronization between threads is at the level of a single process, and interprocess synchronization disappears? That is, in such cases it is no different from lock?

2) A question similar to the previous one, Semaphore without specifying a name, it turns into an ordinary loc only with support for synchronization of several streams, well, it is understandable here. Then it turns out from SemaphoreSlim it differs only in more complex creation because Semaphore (without a name) is created at the kernel level and as a result (due to the lack of a name) it works only for one process (that is, the sense that it is created at levels there is no kernel). So it turns out that SemaphoreSlim and Semaphore (without a name) will work the same way, except that the first is a little faster. Do I understand the alignment correctly? Or is there any difference?

  • 2
    And what have the core of the OS? :-) - Vladimir Martyanov
  • Well, as I understand it, the area of ​​the code, access to which is synchronized using Mutex, is located in the Windows memory. Not inside the managed heap that the CLR has requested from Windows. And access to this area is through the core. Well, I may be wrong) - Snuppi
  • In any case, I rename the topic) - Snuppi
  • With the help of Mutex, you can synchronize not only access to the code :-) - Vladimir Martyanov
  • Hmm, do not quite understand what you mean. If you have a type of synchronization of access to any file, then this is the same code, just a code to write data to a file, for example) We are talking about ordinary C # code, the code is placed between the WaitOne () and Release () methods. As a result, this code becomes a shared resource, access to which is carried out - Snuppi

1 answer 1

  1. Yes, the meaning of the unnamed Mutex is practically the same as lock through lock . The difference is that

    • lock does not work across AppDomain boundaries, and the unnamed Mutex is available throughout the process.
    • Mutex is WaitHandle 'om, that is, you can, for example, wait for it with a timeout or wait for one of several Mutex 's to be blocked.
    • lock implemented better than Mutex .
  2. The difference between Semaphore and SemaphoreSlim about the same. Semaphore - wrapper over a Win32 object. SemaphoreSlim significantly faster, but it works only inside the process and does not export WaitHandle .

  • one
    The phrase Mutex являСтся WaitHandle'ΠΎΠΌ, Ρ‚ΠΎ Π΅ΡΡ‚ΡŒ Π²Ρ‹ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, Π΄ΠΎΠΆΠΈΠ΄Π°Ρ‚ΡŒΡΡ Π΅Π³ΠΎ с Ρ‚Π°ΠΉΠΌΠ°ΡƒΡ‚ΠΎΠΌ ΠΈΠ»ΠΈ ΠΆΠ΄Π°Ρ‚ΡŒ Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΠΈ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΈΠ· Π½Π΅ΡΠΊΠΎΠ»ΡŒΠΊΠΈΡ… Mutex'ΠΎΠ². incomprehensible, correct, please - Andrey NOP
  • @Andrey: Yeah, fixed it, thanks! - VladD