当前位置:网站首页>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 .
边栏推荐
- 目录相关命令
- 【系统分析师之路】第五章 复盘软件工程(软件过程改进)
- Data Lake (11): Iceberg table data organization and query
- Service online governance
- Multi terminal collaboration of Huawei accounts to create a better internet life
- Goods and services - platform properties
- 【系统分析师之路】第五章 复盘软件工程(敏捷开发)
- 2022-06-23 sail soft part formula and SQL generation (month and quarter retrieval)
- (8)JMeter元件详解之 Once only Controller 仅一次控制器
- Development of unity script program
猜你喜欢

A keepalived high availability accident made me learn it again!

Multi terminal collaboration of Huawei accounts to create a better internet life

损失函数:DIOU loss手写实现

WTM重大更新,多租户和单点登录

VisualStudio and SQL

防火墙基础之总部双机热备与分支基础配置
![[KALI] KALI系统、软件更新(附带镜像源)](/img/ac/43a3f81d50ab6866271b500b142252.png)
[KALI] KALI系统、软件更新(附带镜像源)

Google Earth Engine(GEE)——将字符串的转化为数字并且应用于时间搜索( ee.Date.fromYMD)

半导体动态杂谈

The independent station is Web3.0. The national "14th five year plan" requires enterprises to build digital websites!
随机推荐
想请教一下,我在佛山,到哪里开户比较好?手机开户是安全么?
一条查询SQL是如何执行的
ERROR: Cannot uninstall ‘PyYAML‘. It is a distutils installed project and thus we cannot accurately
Knowledge dissemination cannot replace professional learning!
Unity animator parameter
Loss function: Diou loss handwriting implementation
Google Earth Engine(GEE)——GHSL:全球人类住区层,建成网格 1975-1990-2000-2015 (P2016) 数据集
ABAP toolbox v1.0 (with implementation ideas)
rxjs Observable 两大类操作符简介
Basic syntax of unity script (1) - common operations of game objects
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
MySQL如何将列合并?
Why can't the database table be written into data
优思学院:六西格玛不只是统计!
Jetpack Compose 实现完美屏幕适配
【系统分析师之路】第五章 复盘软件工程(敏捷开发)
Lucky hash quiz system development (source code deployment) fun investment hash game play development (case requirements)
(8)JMeter元件详解之 Once only Controller 仅一次控制器
Observable, seulement fiable: première bombe de salon de la série cloudops d'exploitation et d'entretien automatisés dans le nuage
VisualStudio and SQL