In depth analysis of c mutex mutex

  • 2020-06-07 05:14:07
  • OfStack

Mutex (Mutex)

A mutex is a mutex synchronous object, meaning that only one thread can acquire it at the same time.

A mutex is suitable for situations where a Shared resource can only be accessed by one thread at a time


// Creates a mutex in an uncaptured state

Public Mutex ();

// If owned is true, the initial state of the mutex is captured by the main thread, otherwise it is not captured

Public Mutex (bool owned);

If you want to get a mutex. You should call the WaitOne() method on the mutex, which is inherited from the Thread.WaitHandle class

It waits until the called mutex is available, so the method will hold the calling thread until the specified mutex is available. If the mutex is not required, the method ReleaseMutex is used to release the mutex so that the mutex can be acquired by another thread

//Public Mutex (bool owned,name,out flag);

name is the name of the mutex, which means there is only one mutex named name in the operating system. If one thread gets the mutex of name, other threads cannot get the mutex and must wait for the thread to release the mutex

Reference code experiments on other people's blogs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace myConApp
    class Test
        /// <summary>
        ///  The main entry point to the application. 
        /// </summary>
        static void Main(string[] args)
            bool flag = false;
            System.Threading.Mutex mutex = new System.Threading.Mutex(true, "Test", out flag);
            // The first 1 A parameter :true-- Initial ownership of the mutex given to the calling thread 
            // The first 1 A parameter : The name of the mutex 
            // The first 3 A parameter : The return value , If the calling thread has been granted initial ownership of the mutex , It returns true
            if (flag)
                Console.Write("Another is Running");
                System.Threading.Thread.Sleep(5000);// Thread hanging 5 seconds 
                Environment.Exit(1);// Exit the program 

Run the first instance of the application generated by the above code, and you will get the result

Keep the first run state, run the second instance, and get the result

Another is Running
One mutex is created in the above code, and from the explanation of its parameters, the first calling thread will get the initial ownership of the mutex, if not released, the other threads will not get ownership of the mutex

1. Run two projects and put the above codes into the project at the same time. Run the first project and get the result

Keep the first running state, run the second project, and get the results

Another is Running

System.Threading.Mutex mutex = new System.Threading (true, "Test", out flag); Instead of

System.Threading.Mutex mutex = new System.Threading.Mutex(true, "Test1", out flag);

And then the first project runs, and you get the result

Keep the first running state, run the second project, and get the results

In the system, name of mutex is unique in the system.

Related articles: