C#의 Mutex 클래스는 프로세스 간 동기화에도 사용할 수 있는 동기화 기본 요소입니다.
새 Mutex를 만드는 방법을 살펴보겠습니다.
private static Mutex m = new Mutex();
이제 부울 값으로 Mutex 클래스의 새 인스턴스를 초기화하는 방법을 살펴보겠습니다.
private static Mutex m = new Mutex(true);
이제 부울 값과 Mutex 이름을 사용하여 Mutex 클래스의 새 인스턴스를 초기화하는 방법을 살펴보겠습니다.
예시
using System;
using System.Threading;
public class Demo {
public static void Main() {
Mutex mt = new Mutex(false, "NewMutex");
Console.WriteLine("Waiting for the Mutex.");
mt.WaitOne();
Console.WriteLine("Owner of the mutex. " + "ENTER is to be pressed to release the mutexand exit.");
Console.ReadLine();
mt.ReleaseMutex();
}
} 출력
Waiting for the Mutex. Owner of the mutex. ENTER is to be pressed to release the mutex and exit.