当前位置:网站首页>Deep understanding Net (2) kernel mode 3 Kernel mode construct mutex
Deep understanding Net (2) kernel mode 3 Kernel mode construct mutex
2022-06-30 13:49:00 【Cucumbers need programming】
In depth understanding of .Net The construction mode of thread synchronization in ( Two ) Kernel mode construction
Preface
Kernel-mode Constructs Generally translated as kernel mode construction ,
Constructs It is both a noun and a verb , But the translation for construction feels a bit like a verb , I think it's better to translate it into a noun , After all s Of , It means multiple , Verbs obviously can't express more than one , Such as kernel mode constructs , Kernel mode constructs .
Environmental statement
IDE:Visual Studio 2022
OS:Win10
.NET:.Net4.6.1
One 、 What is a mutex ?
mutex mutex Is a mutually exclusive lock , and AutoResetEvent Event lock similar .
It's just mutex Than AutoResetEvent The event lock has some additional functions .mutex The same thread can acquire a lock multiple times , Of course , After multiple fetches, it also needs to be released multiple times to make other threads available , We call this process of multiple lock acquisition recursive lock , Also called reentrant lock . But use mutex When , Frequently switch between kernel code and managed code , This has a certain impact on the performance of the program . It can be used AutoRestEvent Method implements a recursive lock instead of .
But it is true that recursive locks are not used in coding , I can't think of a scene very well .
If it is difficult to recurse, use recursion lock ? It seems to make sense !
Two 、 Code writing
1. Write a foundation AutoRestEvent The recursive lock of
It is mainly used to understand the implementation of recursive lock , Compared with ordinary event locks , Mainly increased 2 Variables to implement , One is to record the thread that obtains the lock ID Variable , One is a variable that records the number of times a lock is acquired .
The code is as follows ( Example ):
internal sealed class RecursiveAutoResetEvent : IDisposable
{
private AutoResetEvent m_lock = new AutoResetEvent(true);
private Int32 m_owningThreadId = 0;
private Int32 m_recursionCount = 0;
public void Enter()
{
// Obtain the calling thread's unique Int32 ID
// Get a unique thread ID
Int32 currentThreadId = Thread.CurrentThread.ManagedThreadId;
// If the calling thread owns the lock, increment the recursion count
//
if (m_owningThreadId == currentThreadId)
{
m_recursionCount++;
return;
}
// The calling thread doesn't own the lock, wait for it
m_lock.WaitOne();
// The calling now owns the lock, initialize the owning thread ID & recursion count
m_owningThreadId = currentThreadId;
m_recursionCount--;
}
public void Leave()
{
// If the calling thread doesn't own the lock, we have an error
if (m_owningThreadId != Thread.CurrentThread.ManagedThreadId)
throw new InvalidOperationException();
// Subtract 1 from the recursion count
if (--m_recursionCount == 0)
{
// If the recursion count is 0, then no thread owns the lock
m_owningThreadId = 0;
m_lock.Set(); // Wake up 1 waiting thread (if any)
}
}
public void Dispose() {
m_lock.Dispose(); }
}
summary
Recursive locks are rarely used , and mutex The recursive lock of exists many times from managed code to kernel code , The performance is theoretically lower than AutoResetEvent The recursive lock of .
边栏推荐
- 损失函数:DIOU loss手写实现
- 发生QQ大规模盗号事件,暴露出什么网络安全问题?
- (8)JMeter元件详解之 Once only Controller 仅一次控制器
- DNS 解析之家庭网络接入 Public DNS 实战
- 深度长文探讨Join运算的简化和提速
- 科普达人丨漫画图解什么是eRDMA?
- A keepalived high availability accident made me learn it again!
- 【招聘(广州)】成功易(广州).Net Core中高级开发工程师
- 一次 Keepalived 高可用的事故,让我重学了一遍它!
- Mqtt ROS simulates publishing a custom message type
猜你喜欢

Assertions of regular series

MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video

深入理解.Net中的线程同步之构造模式(二)内核模式2.内核模式构造物Semaphone

The independent station is Web3.0. The national "14th five year plan" requires enterprises to build digital websites!

SQL编程问题,测试用例不通过

More than 20 years after Hong Kong's return, Tupu digital twin Hong Kong Zhuhai Macao Bridge has shocked

重磅:国产IDE发布,由阿里研发,完全开源!

Rk356x u-boot Institute (command section) 3.2 usage of help command

【科学文献计量】外文文献及中文文献关键词的挖掘与可视化

What is erdma as illustrated by Coptic cartoon?
随机推荐
今日睡眠质量记录80分
How to handle ZABBIX server startup failure
Unity Animator 参数
In the digital age, XDR (extended detection and response) has unlimited possibilities
科普达人丨漫画图解什么是eRDMA?
想请教一下,我在佛山,到哪里开户比较好?手机开户是安全么?
随着产业互联网的发展,有关互联网的落地和应用也就变得宽阔了起来
用Unity实现Flat Shading
Product manager professional knowledge 50 (7) - how to establish a complete set of user growth system?
Embedded development: five C features that may no longer be prohibited
Unity 频繁切换分支 结果模型出现莫名其妙的错误
【招聘(广州)】成功易(广州).Net Core中高级开发工程师
(8)JMeter元件详解之 Once only Controller 仅一次控制器
[the path of system analyst] Chapter 5 Software Engineering (Agile Development)
RK356x U-Boot研究所(命令篇)3.2 help命令的用法
单元测试效率优化:为什么要对程序进行测试?测试有什么好处?
Goods and services - platform properties
DNS 解析之家庭网络接入 Public DNS 实战
There is no utf8 option for creating tables in Navicat database.
Click the TD cell of table to open the dialog pop-up window. After obtaining the value, put the value back into the TD cell