当前位置:网站首页>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 .
边栏推荐
- Basic syntax of unity script (3) - accessing game object components
- 我想问一下招商证券怎么开户?通过链接办理股票开户安全吗
- MySQL如何将列合并?
- IM即时通讯应用开发中无法解决的“顽疾”
- Pytorch查看模型参数量和计算量
- Hangzhou E-Commerce Research Institute: the official website (website) is the only form of private domain
- Jetpack Compose 实现完美屏幕适配
- [KALI] KALI系统、软件更新(附带镜像源)
- ABAP工具箱 V1.0(附实现思路)
- 【招聘(广州)】成功易(广州).Net Core中高级开发工程师
猜你喜欢
随机推荐
逆向调试入门-PE中的VA与RVA换算04/07
深入理解.Net中的线程同步之构造模式(二)内核模式3.内核模式构造物Mutex
Basic syntax of unity script (4) - access to other game objects
Kaniko official documents - build images in kubernetes
get请求与post提交区别的简易理解
How can I protect my private key?
这个编辑器即将开源!
MySQL如何将列合并?
一篇文章读懂关于企业IM的所有知识点
navicat数据库建表是没有utf8选项。
[the path of system analyst] Chapter V software engineering (software process improvement)
Hangzhou E-Commerce Research Institute: the official website (website) is the only form of private domain
服务线上治理
【刷题篇】供暖器
一条查询SQL是如何执行的
Dart 扩展特性
深入理解.Net中的线程同步之构造模式(二)内核模式2.内核模式构造物Semaphone
智慧运维:基于 BIM 技术的可视化管理系统
Defi "where does the money come from"? A problem that most people don't understand
There is no utf8 option for creating tables in Navicat database.









