当前位置:网站首页>深入理解.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调试界面
我们可以分析代码得知,获取锁后,我们不会释放锁,这个时候我们重新开启一个程序(可以视作一个进程),这时,进程获取不到锁,因为在另外一个进程中没有锁释放。
这个就是跨进程同步
边栏推荐
- Basic syntax of unity script (2) -record time in unity
- Assertions of regular series
- MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
- Yousi College: Six Sigma is not just statistics!
- Unity Animator 参数
- Inexplicable error occurred in unity's frequent switching branch result model
- 数字时代,XDR(扩展检测与响应)的无限可能
- 【C】 In depth understanding of pointers and callback functions (Introduction to simulating qsort)
- Basic syntax of unity script (3) - accessing game object components
- All the abnormal knowledge you want is here
猜你喜欢
可觀測,才可靠:雲上自動化運維CloudOps系列沙龍 第一彈
顺应媒体融合趋势,中科闻歌携手美摄打造数智媒宣
[kali] Kali system, software update (with image source)
步骤详解 | 助您轻松提交 Google Play 数据安全表单
visualstudio 和sql
ABAP工具箱 V1.0(附实现思路)
可观测,才可靠:云上自动化运维CloudOps系列沙龙 第一弹
[Title brushing] coco, who likes bananas
SQL编程问题,测试用例不通过
Observable, seulement fiable: première bombe de salon de la série cloudops d'exploitation et d'entretien automatisés dans le nuage
随机推荐
华为帐号多端协同,打造美好互联生活
How can c write an SQL parser
Defi "where does the money come from"? A problem that most people don't understand
Directory related commands
智慧运维:基于 BIM 技术的可视化管理系统
正则系列之断言Assertions
Inexplicable error occurred in unity's frequent switching branch result model
Loss function: Diou loss handwriting implementation
科普达人丨漫画图解什么是eRDMA?
【科研数据处理】[基础]类别变量频数分析图表、数值变量分布图表与正态性检验(包含对数正态)
[deep anatomy of C language] storage principle of float variable in memory & comparison between pointer variable and "zero value"
SQL programming problem, test case failed
用Unity实现Flat Shading
【C】 In depth understanding of pointers and callback functions (Introduction to simulating qsort)
逆向调试入门-PE中的VA与RVA换算04/07
Basic syntax of unity script (1) - common operations of game objects
How to take the first step in digital transformation
A keepalived high availability accident made me learn it again!
Why can't the database table be written into data
Basic syntax of unity script (5) - vector