C# 里的 Mutex 类是什么?
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.
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP