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.
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP