当前位置:网站首页>深入理解.Net中的线程同步之构造模式(二)内核模式4.内核模式构造物的总结
深入理解.Net中的线程同步之构造模式(二)内核模式4.内核模式构造物的总结
2022-06-30 13:10:00 【小黄瓜要编程】
深入理解.Net中的线程同步之构造模式(二)内核模式构造
前言
Kernel-mode Constructs一般翻译为内核模式构造 ,
Constructs 即是一个名词也是一个动词,但是翻译为构造感觉有点像一个动词,个人感觉翻译为名词更好,毕竟是加了s的,就表示多个,动词显然不能表示多个啊,比如内核模式构造物,内核模式构造体。
环境说明
IDE:Visual Studio 2022
OS:Win10
.NET:.Net4.6.1
一、内核模式的优点
我们前面3个小节简单的过了一个内核构造物的三个常见的构造物,现在我们就来说说为什么要使用内核构造物?内核构造物的优点?
内核模式的一个很重要的优点是可以进行跨进程线程同步(我们限制我们的应用只能运行一个程序)
相比用户模式的自旋,内核模式构造物会进行线程的阻塞
可以在托管线程和本地线程之间相互同步
可以基于EventWaitHandleSecurity进行一定的线程访问权限控制
二、代码编写
1.基于EventWaitHandle改造一个简单的锁
代码如下(示例):
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($"线程ID:{
currentThreadId},{
DateTime.Now},阻塞等待开始");
// Block in kernel until resource available
m_available.WaitOne();
Console.WriteLine($"线程ID:{
currentThreadId},{
DateTime.Now},阻塞等待完成");
}
public void Leave()
{
// Let another thread access the resource
m_available.Set();
}
public void Dispose()
{
m_available.Dispose();
}
}
2.测试InterProcessLockTest跨线程锁的效果
代码如下(示例):
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 !");
}
}
我们先编辑完成,进入debug目录下直接运行控制台程序,可以看见如下
然后再启动visual studio调试界面
我们可以分析代码得知,获取锁后,我们不会释放锁,这个时候我们重新开启一个程序(可以视作一个进程),这时,进程获取不到锁,因为在另外一个进程中没有锁释放。
这个就是跨进程同步
边栏推荐
- 用Unity实现Flat Shading
- Unity animator parameter
- Exlipse operates on multiple rows at the same time. For example, input the same text in multiple lines and columns at the same time
- Apache Doris Compaction优化百科全书
- kaniko官方文档 - Build Images In Kubernetes
- Clearing TinyMCE rich text cache in elementui
- 服务线上治理
- RK356x U-Boot研究所(命令篇)3.2 help命令的用法
- Golang foundation -- slicing several declaration methods
- [Title brushing] heater
猜你喜欢

华为帐号多端协同,打造美好互联生活

JMeter learning notes

智慧运维:基于 BIM 技术的可视化管理系统
![[Select] resource realization information, news, we media, blog applet (can be drained, open traffic master, with PC background management)](/img/e7/1c34d8aa364b944688ec2ffb4feb7c.jpg)
[Select] resource realization information, news, we media, blog applet (can be drained, open traffic master, with PC background management)

【刷题篇】爱吃香蕉的珂珂

Pytorch查看模型参数量和计算量

All the abnormal knowledge you want is here

香港回归20余年,图扑数字孪生港珠澳大桥,超震撼

In line with the trend of media integration, Zhongke Wenge and Meishe jointly create digital intelligence media publicity

半导体动态杂谈
随机推荐
Exploring the source code of Boca Cross Chain Communication: Elements
[the path of system analyst] Chapter 5 Software Engineering (Agile Development)
Google Earth Engine(GEE)——GHSL:全球人类住区层,建成网格 1975-1990-2000-2015 (P2016) 数据集
Dart 扩展特性
Waving flags and shouting for basic language
Exlipse operates on multiple rows at the same time. For example, input the same text in multiple lines and columns at the same time
visualstudio 和sql
单元测试效率优化:为什么要对程序进行测试?测试有什么好处?
[Title brushing] heater
Google Earth Engine(GEE)——将字符串的转化为数字并且应用于时间搜索( ee.Date.fromYMD)
rxjs Observable 两大类操作符简介
Kaniko official documents - build images in kubernetes
kaniko官方文档 - Build Images In Kubernetes
可觀測,才可靠:雲上自動化運維CloudOps系列沙龍 第一彈
Idea 2021.3 golang error: rning: undefined behavior version of delve is too old for go version 1.18
ABAP toolbox v1.0 (with implementation ideas)
Apache Doris Compaction优化百科全书
On the simplification and acceleration of join operation
提权扫描工具
[Select] resource realization information, news, we media, blog applet (can be drained, open traffic master, with PC background management)