Forwarded from: http://www.cnblogs.com/HouZhiHouJueBlogs/p/3945195.html
A mutex is a kernel object that is used to ensure that a thread is exclusive of access to a resource, and that the mutex can be used for mutually exclusive access to resources in different processes.
We can think of a mutex as a taxi and a passenger as a thread. Passengers first wait for the bus, then get on the bus and finally alight. When a passenger is in the car, the other passengers are only allowed to get on when he gets off the bus. The same is true of the relationship between a thread and a C # mutex object, where the thread waits for the C # mutex object to be disposed using the Mutex.waitone () method, or if it waits for a C # mutex object to be freed, or if it is not useful to any object, it automatically owns the object. Until it calls the Mutex.releasemutex () method to release the object, during which other threads that want to get the C # mutex object are waiting.
MSDN Address: Http://msdn.microsoft.com/en-us/library/system.threading.mutex (v=vs.110). aspx
The example on MSDN shows:
Using system;using system.threading;class example{//Create a new Mutex. The creating thread does not own the mutex. private static Mutex Mut = new Mutex (); Private Const int numiterations = 1; Private Const int numthreads = 3; static void Main () {//Create the threads that would use the protected resource. for (int i = 0; i < numthreads; i++) {Thread newthread = new Thread (new ThreadStart (ThreadProc)); Newthread.name = String.Format ("thread{0}", i + 1); Newthread.start (); }//The main thread exits, but the application continues to//run until all foreground threads has Exite D.} private static void ThreadProc () {for (int i = 0; i < numiterations; i++) {UseR Esource (); }}//This method represents a resource it must be synchronized//so that is only one thread at a time can Ente R. private static void Useresource () {Wait until it is safe to enter, and does not enter if the request times out. Console.WriteLine ("{0} is requesting the mutex", Thread.CurrentThread.Name); if (Mut. WaitOne (+) {Console.WriteLine ("{0} have entered the protected area", Thread.CurrentThread.Nam e); Place code to access non-reentrant resources here. Simulate some work. Thread.Sleep (5000); Console.WriteLine ("{0} is leaving the protected area", Thread.CurrentThread.Name); Release the Mutex. Mut. ReleaseMutex (); Console.WriteLine ("{0} has released the mutex", Thread.CurrentThread.Name); } else {Console.WriteLine ("{0} would not acquire the mutex", Thread.currentt Hread. Name); }}}//The example displays output like the following://Thread1 are requesting the mutex//Thread1 has en tered theProtected Area//Thread2 are requesting the mutex//THREAD3 is requesting the mutex//Thread2 would not Acquire the mutex//THREAD3 would not acquire the mutex//Thread1 are leaving the protected area//THR Ead1 has released the mutex
When a thread occupies a mutex, the code can write:
Mutex. WaitOne (); Mutex. WaitOne (); Mutex. WaitOne (); Mutex. WaitOne (); .....
WaitOne when this method is called, the system checks to see that the mutex is not being used by any thread and therefore assigns the mutex to the current thread
Thus, even if you continue to invoke WaitOne, there is no effect because the system discovers that the current thread is already occupied and returns directly. In other words, WaitOne is getting
The way the mutex locks. If you call ReleaseMutex then when the thread exits the mutex lock, other threads can come in to apply. But if you call two times
ReleaseMutex then the exception is thrown because the current thread is actually not occupying the lock.
So it can't be written like this:
Mutex. ReleaseMutex ();//If it already occupies so Okmutex.releasemutex ();//Call the second direct exception
A mutex is a kernel object, so it can be used as a cross-process thread synchronization.
A process can be written like this:
Mutex mutex = new Mutex (true, "mutex1");
The b process can be written like this:
Mutex mutex = mutex.openexisting ("Mutex1")
OpenExisting This method signature we can often see later that the function is to get an existing kernel object.
The thread synchronization code that gets to the following is consistent with the single process.
Complete.
Use of mutexes in C # multithreaded series