当前位置:网站首页>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本身是可以系统级别的,所以是可以跨越进程的。
边栏推荐
- RS485 serial port wiring description of smart lamp post smart gateway
- 潞晨科技获邀加入NVIDIA初创加速计划
- 创客教育给教师发展带来的挑战
- How does easyplayer RTSP configure sending heartbeat information to the server?
- Do you know about Statistics?
- Brief introduction of domain name registration
- 浅谈如何运营好小红书账号:利用好长尾词理论
- Easy car Interviewer: talk about MySQL memory structure, index, cluster and underlying principle!
- About Stacked Generalization
- 线程安全与实现方法
猜你喜欢

File system notes

程序员使用个性壁纸

文件系统笔记
![跳跃游戏II[贪心练习]](/img/e4/f59bb1f5137495ea357462100e2b38.png)
跳跃游戏II[贪心练习]

Database stored procedure begin end

leetcode:85. 最大矩形

leetcode:剑指 Offer 26:判断t1中是否含有t2的全部拓扑结构

C language student management system - can check the legitimacy of user input, two-way leading circular linked list
![[binary tree] - middle order traversal of binary tree](/img/93/442bdbecb123991dbfbd1e5ecc9d64.png)
[binary tree] - middle order traversal of binary tree

目标5000万日活,Pwnk欲打造下一代年轻人的“迪士尼乐园”
随机推荐
Printer connection mode
If you want to learn programming well, don't recite the code!
Station B collapsed. Let's talk to the injured programmers
Easyscreen live streaming component pushes RTSP streams to easydss for operation process sharing
Operation and maintenance dry goods | how to improve the business stability and continuity through fault recovery?
Record -- about the problem of garbled code when JSP foreground passes parameters to the background
Record of waic 2021 round table conference 𞓜 cross border dialogue: current situation and future of AI and sustainable development
Interpreting the new features of Appstore: Customizing product pages and a/b test tools
35岁危机?内卷成程序员代名词了
What are the easy-to-use character recognition software? Which are the mobile terminal and PC terminal respectively
Do you know about Statistics?
Free and easy-to-use screen recording and video cutting tool sharing
Papamelon 11 number of calculation permutation [combinatorics]
RealNetworks vs. 微软:早期流媒体行业之争
Why the computer can't start
How fast? Good province!
Do you want to research programming? I got six!
mysql中的 ON UPDATE CURRENT_TIMESTAMP
Easynvr is optimized when a large number of videos are not online or unstable due to streaming failure
How long does the domain name filing take and what materials need to be prepared