当前位置:网站首页>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 .
边栏推荐
- 【招聘(广州)】成功易(广州).Net Core中高级开发工程师
- 随着产业互联网的发展,有关互联网的落地和应用也就变得宽阔了起来
- Google Earth Engine(GEE)——GHSL:全球人类住区层,建成网格 1975-1990-2000-2015 (P2016) 数据集
- Read all the knowledge points about enterprise im in one article
- 【系统分析师之路】第五章 复盘软件工程(软件过程改进)
- More than 20 years after Hong Kong's return, Tupu digital twin Hong Kong Zhuhai Macao Bridge has shocked
- get请求与post提交区别的简易理解
- 数字化转型道阻且长,如何迈好关键的第一步
- MySQL access denied, opened as Administrator
- MySQL queries the data within the radius according to the longitude and latitude, and draws a circle to query the database
猜你喜欢
随机推荐
步骤详解 | 助您轻松提交 Google Play 数据安全表单
Paper interpretation (AGC) attributed graph clustering via adaptive graph revolution
[recruitment (Guangzhou)] Chenggong Yi (Guangzhou) Net core middle and Senior Development Engineer
Embedded development: five C features that may no longer be prohibited
Google Earth Engine(GEE)——GHSL:全球人类住区层,建成网格 1975-1990-2000-2015 (P2016) 数据集
PG基础篇--逻辑结构管理(表继承、分区表)
IM即时通讯应用开发中无法解决的“顽疾”
Basic syntax of unity script (1) - common operations of game objects
This editor will open source soon!
MySQL queries the data within the radius according to the longitude and latitude, and draws a circle to query the database
mysql拒绝访问、管理员身份打开的
Intelligent operation and maintenance: visual management system based on BIM Technology
服务线上治理
Common UI components
可觀測,才可靠:雲上自動化運維CloudOps系列沙龍 第一彈
Golang template (text/template)
重磅:国产IDE发布,由阿里研发,完全开源!
【系统分析师之路】第五章 复盘软件工程(软件过程改进)
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
点击table的td单元格出现dialog弹窗,获取值后将值放回td单元格