What are Mutexes and when is it advisable to use this feature?

Asked

Viewed 672 times

5

I read about Mutexes and the class Mutex. However, I cannot understand clearly what Mutex is and what this class does.

Since I would like to know if I can use this feature to prevent my desktop application from running more than once (multiple instances of the same process in case the user opens the program repeatedly).

An implementation I did was as follows:

private bool ProcessoExecutando()
{
    var localizacao = Assembly.GetExecutingAssembly().Location;
    FileSystemInfo systemInfo = new FileInfo(localizacao);
    Mutex mutex = new Mutex(true, "Global\\" + systemInfo.Name, out bool novoProcessoCriado);
    if (novoProcessoCriado)
    {
        mutex.ReleaseMutex();
    }
    return !novoProcessoCriado;
}

This method is called in the method Main() and serves in thesis to verify if there is already an instance of the running application process. And it seems to me a little gambiarra =/

Behold:

static void Main()
{
    if (ProcessoExecutando())
    {
        return;
    }

    //Inicia aplicação
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);            
    Application.Run(new AppMain());
}

However, the meaning of the class Mutex this confused in my mind and my lack of knowledge in it can cause me to use this resource in the wrong way.

Therefore, I would like to have the doubts clarified below.

Doubts

  1. What are Mutexes?
  2. When it is advisable to use the class Mutex?
  3. The case illustrated above the class used Mutex is ideal for prevent the application from being executed more than once?

1 answer

4


What are Mutexes?

The Mutual Exclusion mechanism is used in concurrent environments and is a way to prevent a resource from being accessed by more than one processing line at the same time. It is mainly used to prevent the running condition, to maintain an operation atomic.

It is a form of locking a resource, in general in memory. There are other similar mechanisms such as semaphore, for example, where I have already given an answer and talked superficially about Mutex. The wikipedia article shows some.

While any resource cannot be accessed concurrently it does not need such a mechanism, and it is often preferable to have some form so that it is much simpler, even if it needs to duplicate the resource. Creating a Mutex is not cheap, and every feature that can only be accessed with a query to Mutex has a much more expensive access than normal, in some cases this control is more expensive than the access itself. But not all cases can have concurrent access eliminated, there are objects that need a consistent global state.

There are cases where an object can have an internal Mutex, in others it can be used in an individualized way. The internal use can give more guarantee that no access will be violated, but requires the object to have the cost of this control in all accesses, even when there is no competition.

Mutex, in general, is an object that indicates whether the object is being accessed by some code at that time or not. So you always need to consult him to find out if you can do something on the object. If the query is not made and trying to access the object without care may cause problems. You can only freely access the object if Mutex is released and this should only occur when the person who created it does.

Only one processing line can own a Mutex for a resource.

Understand Mutex as a doorman who tells you who can and can’t get into a place and he only lets you in if the place is vacant. It knows environments where everyone wants to talk and only one can. Someone controls the microphone that you can talk about at that time. That’s Mutex. This particular case only those who can say that someone else can use the microphone are those who are using it. Only it can turn off the use and allow someone else to start using. The other person may be trying, but has no access to it while the other is using.

In C# you can see the code of the class implementing Mutex. Or you can see what it’s like in . NET Core, very different and need to follow abstractions since each operating system is different.

Threads tentando acessar um objeto

When it is advisable to use the Mutex class?

In essence what has already been said. There are examples in links for answers already posted here on Sopt that show this.

The above illustrated case of the Mutex class is ideal to prevent the application from running more than once?

It does not seem to me to be necessary a Mutex, it is possible to know that the application is running in other ways, and the cost of this is usually already quite high, I do not need a low cost mechanism that is the case of Mutex.

Normally a Mutex should last very little time under pain of causing deadlocks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.