当前位置:网站首页>Deep understanding Net (2) kernel mode 4 Summary of kernel pattern constructs
Deep understanding Net (2) kernel mode 4 Summary of kernel pattern constructs
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 、 The advantages of kernel mode
Ahead of us 3 The first section briefly introduces three common constructs of a kernel construct , Now let's talk about why we use kernel constructs ? Advantages of kernel constructs ?
An important advantage of kernel mode is that it can synchronize threads across processes ( We restrict our application to only one program )
Compared to the user mode spin , Kernel mode constructs block threads
You can synchronize between managed threads and local threads
Can be based on EventWaitHandleSecurity Perform certain thread access control
Two 、 Code writing
1. be based on EventWaitHandle Transform a simple lock
The code is as follows ( Example ):
public sealed class EventLock : IDisposable
{
private readonly EventWaitHandle m_available;
public EventLock()
{
m_available = new EventWaitHandle(true,EventResetMode.AutoReset,"testEvent"); // Initially free
}
public void Enter()
{
Int32 currentThreadId = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine($" Threads ID:{
currentThreadId},{
DateTime.Now}, Blocking wait start ");
// Block in kernel until resource available
m_available.WaitOne();
Console.WriteLine($" Threads ID:{
currentThreadId},{
DateTime.Now}, Blocking waiting for completion ");
}
public void Leave()
{
// Let another thread access the resource
m_available.Set();
}
public void Dispose()
{
m_available.Dispose();
}
}
2. test InterProcessLockTest The effect of cross thread locking
The code is as follows ( Example ):
internal class Program
{
static void Main(string[] args)
{
new InterProcessLockTest().TestStart();
Console.ReadLine();
}
}
public class InterProcessLockTest
{
public void TestStart()
{
Console.WriteLine(" InterProcessLockTest TestStart Start !");
IntObj intObj = new IntObj();
EventLock eventLock = new EventLock();
eventLock.Enter();
Console.WriteLine($"get lock,{
DateTime.Now}");
Thread.Sleep(1000*30);
Console.WriteLine($"wait 30s,{
DateTime.Now}");
Console.WriteLine(" InterProcessLockTest TestStart END !");
}
}
Let's finish the editing first , Get into debug Run the console program directly under the directory , You can see the following 
And then it starts visual studio Debugging interface 
We can analyze the code to know , After acquiring a lock , We will not release the lock , At this time, we restart a program ( Can be seen as a process ), At this time , The process could not acquire the lock , Because there is no lock release in another process .
This is cross process synchronization
边栏推荐
- Observable, reliable: the first shot of cloudops series Salon of cloud automation operation and maintenance
- DeFi“钱从哪来”?一个大多数人都没搞清楚的问题
- Mqtt ROS simulates publishing a custom message type
- In the digital age, XDR (extended detection and response) has unlimited possibilities
- In line with the trend of media integration, Zhongke Wenge and Meishe jointly create digital intelligence media publicity
- 目录相关命令
- Basic syntax of unity script (2) -record time in unity
- 华为帐号多端协同,打造美好互联生活
- ABAP toolbox v1.0 (with implementation ideas)
- Methodology for troubleshooting problems (applicable to troubleshooting problems arising from any multi-party cooperation)
猜你喜欢

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

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

提权扫描工具

【C】 In depth understanding of pointers and callback functions (Introduction to simulating qsort)

QQ 居然被盗了?原因在这......

ABAP toolbox v1.0 (with implementation ideas)

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

SQL考勤统计月报表

Kaniko official documents - build images in kubernetes

深入理解.Net中的线程同步之构造模式(二)内核模式2.内核模式构造物Semaphone
随机推荐
Step by step | help you easily submit Google play data security form
Common UI components
Observable, reliable: the first shot of cloudops series Salon of cloud automation operation and maintenance
华为帐号多端协同,打造美好互联生活
How to take the first step in digital transformation
rxjs Observable 两大类操作符简介
Basic syntax of unity script (2) -record time in unity
get请求与post提交区别的简易理解
我想问一下招商证券怎么开户?通过链接办理股票开户安全吗
深入理解.Net中的线程同步之构造模式(二)内核模式2.内核模式构造物Semaphone
SQL编程问题,测试用例不通过
[the path of system analyst] Chapter V software engineering (software process improvement)
我如何才能保护我的私钥?
ABAP工具箱 V1.0(附实现思路)
DNS resolution home network access public DNS practice
Embedded development: five C features that may no longer be prohibited
RK356x U-Boot研究所(命令篇)3.2 help命令的用法
Resource realization applet opening wechat official small store tutorial
Directory related commands
目录相关命令