当前位置:网站首页>c#:互斥锁的使用
c#:互斥锁的使用
2022-06-24 06:39:00 【机器视觉001】
1. 互斥锁lock
定义:
private static readonly object m_objLock = new object();
使用:
lock (m_objLock)
{
// todo
}
作用:将会锁住代码块的内容,并阻止其他线程进入该代码块,直到该代码块运行完成,释放该锁。注意:定义的锁对象应该是:私有+静态+只读+引用类型的对象,这样可以防止外部改变锁对象。
2. 互斥锁Monitor
定义:
private static readonly object m_objLock = new object();
使用:
Monitor.Enter(m_objLock);
//todo
Monitor.Exit(m_objLock);
作用:将会锁住代码块的内容,并阻止其他线程进入该代码块,直到该代码块运行完成,释放该锁。注意:注意:定义的锁对象应该是:私有+静态+只读+引用类型的对象,这样可以防止外部改变锁对象。
Monitor有TryEnter的功能,可以防止出现死锁的问题,lock没有。
3. 互斥锁Mutex
定义:
private static readonly Mutex mutex = new Mutex();
使用:
mutex.WaitOne();
//todo
mutex.ReleaseMutex();
作用:将会锁住代码块的内容,并阻止其他线程进入该代码块,直到该代码块运行完成,释放该锁。注意:注意:定义的锁对象应该是:私有+静态+只读+引用类型的对象,这样可以防止外部改变锁对象。
Mutex本身是可以系统级别的,所以是可以跨越进程的。
边栏推荐
- Cloud native high availability and Disaster Recovery Series (I): pod break up scheduling
- leetcode:85. Max rectangle
- Coding platform project construction guide
- MySQL concept - View
- 基于三维GIS系统的智慧水库管理应用
- With a goal of 50million days' living, pwnk wants to build a "Disneyland" for the next generation of young people
- Page Jump and database connection of student management system
- "Adobe international certification" in the design industry, why can't big but big designs have good results?
- CloudCompare&PCL 点云裁剪(基于裁剪盒)
- Station B collapsed. Let's talk to the injured programmers
猜你喜欢

【二叉树】——二叉树中序遍历

Localized operation on cloud, the sea going experience of kilimall, the largest e-commerce platform in East Africa

记录--关于JSP前台传参数到后台出现乱码的问题

C language student management system - can check the legitimacy of user input, two-way leading circular linked list

leetcode:85. Max rectangle

Database stored procedure begin end

开源与创新
![[JUC series] completionfuture of executor framework](/img/d0/c26c9b85d1c1b0da4f1a6acc6d33e3.png)
[JUC series] completionfuture of executor framework

目标5000万日活,Pwnk欲打造下一代年轻人的“迪士尼乐园”

With a goal of 50million days' living, pwnk wants to build a "Disneyland" for the next generation of young people
随机推荐
【JUC系列】Executor框架之CompletionFuture
typescript vscode /bin/sh: ts-node: command not found
Let's talk about BOM and DOM (5): dom of all large Rovers and the pits in BOM compatibility
leetcode:1856. 子数组最小乘积的最大值
Easyscreen live streaming component pushes RTSP streams to easydarwin for operation process sharing
How to give full play to the advantages of Internet of things by edge computing intelligent gateway
项目Demo
[JUC series] completionfuture of executor framework
Word cannot copy and paste processing method
CloudCompare&PCL 点云裁剪(基于裁剪盒)
C语言学生管理系统——可检查用户输入合法性,双向带头循环链表
Basic knowledge of wechat applet cloud development literacy chapter (I) document structure
Challenges brought by maker education to teacher development
Talk about how to dynamically specify feign call service name according to the environment
Jumping game ii[greedy practice]
Authoritative recognition! Tencent cloud data security Zhongtai was selected as the 2021 pioneer practice case
Application of O & M work order
Page Jump and database connection of student management system
How long does the domain name filing take and what materials need to be prepared
学生管理系统页面跳转及数据库连接