How mutexes are used between processes

What is the difference between lock and mutex?


Reply:


A lock is specific to the AppDomain, while Mutex allows the operating system to lock and synchronize between processes (IPC).


is a compiler keyword, not an actual class or object. It is a wrapper around the functionality of the class and is designed to make working with the general case easier.

Those (and the keyword) are, as Darin said, limited to those. Primarily because a reference to a memory address (in the form of an instantiated object) is required to maintain the "lock" and maintain the identity of the

On the other hand, this is a .Net wrapper around an operating system construct and can be used for system-wide synchronization, whereby string Data (instead of a pointer to data) as an identifier. Two mutexes that reference two strings in two completely different memory locations, but the same Have data , actually use the same mutex of the operating system.


A can either process-local or be system-wide . MSDN:

There are two types of mutex: local mutexes that are not named and named system mutexes. A local mutex only exists in your process.

In addition, special care should be taken when using a system-wide mutex on a system with Terminal Services - also described on the same page.

One of the differences between and is that it uses a kernel-level construct so that synchronization always requires at least one transition between the user area and the kernel area.

- this is really a shortcut to the class, on the other hand tries to avoid allocating kernel resources and switching to kernel code (and is therefore leaner and faster - if you have to find a WinAPI construct that is similar to it, it would be).

The other difference is what others point out: A Surname can can be used across processes.

Unless you have special needs or need synchronization between processes, it is simply better to adhere to (aka)).

There are some other "minor" differences, such as: B. how to deal with leaving etc.

The same goes for and in 3.5 and the new in .NET 4.0 etc. It is true that the latter classes cannot be used as system-wide synchronization primitives, but they were never meant to be - they were "just" meant to be faster and more resource efficient.


I'm using a mutex to check if a copy of the application is already running on the same computer.


Much has been said already, but for the sake of simplicity, here is my opinion.

lock -> Easy to use, wrapper on the monitor, locks threads in an AppDomain.

unnamed mutex -> similar to locking, except that the restricted area is more and extends over AppDomain in one process.

Named Mutex -> Blocked area is even more than an unnamed mutex and cross-process in an operating system.

So now options are available. You have to choose the one that best suits your case.



Mutex is a cross-process process, and there will be a classic example of no more than one instance of an application running.

The second example is that you have a file and you don't want different processes to access the same file. You can implement a mutex, but remember that mutex is an operating system and cannot be used between two remote processes.

The lock is the easiest way to protect part of your code. It is app domain specific. You can replace the lock with monitors if you want controlled synchronization.


Some minor differences not mentioned in the answers:

  1. In the case of using locks, you can be sure that the lock will Approved if an exception occurs within the block of the lock.
    That's because the lock applications monitors under the hood and implements this way:

    Either way, the lock is released and you don't have to manually unlock it (as with the mutexes).

  2. You usually use a for locking private object to lock (and should use it ).
    This happens for many reasons. (For more information: see this answer and official documentation).

So in the case of locks, you can Not Access the locked object (accidentally) from outside and cause damage.
However, this is possible with Mutex because it is common to use a Mutex that is marked public and used by everywhere.

We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.