当前位置:网站首页>深入理解.Net中的线程同步之构造模式(二)内核模式1.内核模式构造物Event事件
深入理解.Net中的线程同步之构造模式(二)内核模式1.内核模式构造物Event事件
2022-06-30 15:33:00 【小黄瓜要编程】
深入理解.Net中的线程同步之构造模式(二)内核模式构造
前言
Kernel-mode Constructs一般翻译为内核模式构造 ,
Constructs 即是一个名词也是一个动词,但是翻译为构造感觉有点像一个动词,个人感觉翻译为名词更好,毕竟是加了s的,就表示多个,动词显然不能表示多个啊,比如内核模式构造物,内核模式构造体。
环境说明
IDE:Visual Studio 2022
OS:Win10
.NET:.Net4.6.1
一、内核模式构造是什么?
内核模式构体造简单的来说就是由Windows内核为我们的提供的一些方法 参数 变量来同步线程,这些构造和window系统密切相关。内核构造虽然相对去用户模式的构造要慢一点,但是不会产生疯狂的自旋,占用CPU资源。
事件和信号量是两种常见的内核构造物。
Event
event其实就是一个由内核维护的bool变量。
二、代码编写
1.编写一个基于event的锁
代码如下(示例):
public sealed class EventLock : IDisposable
{
private readonly AutoResetEvent m_available;
public EventLock()
{
m_available = new AutoResetEvent(true); // Initially free
}
public void Enter()
{
// Block in kernel until resource available
m_available.WaitOne();
}
public void Leave()
{
// Let another thread access the resource
m_available.Set();
}
public void Dispose()
{
m_available.Dispose();
}
}
2.测试锁的效果
代码如下(示例):
public sealed class EventLock : IDisposable
{
private readonly AutoResetEvent m_available;
public EventLock()
{
m_available = new AutoResetEvent(true); // Initially free
}
public void Enter()
{
// Block in kernel until resource available
m_available.WaitOne();
}
public void Leave()
{
// Let another thread access the resource
m_available.Set();
}
public void Dispose()
{
m_available.Dispose();
}
}
效果如下
总结
可以看出加上了事件锁后,确实能保证输出的顺序,而没有加锁的数据是乱序,甚至可能出行脏数据的情况
边栏推荐
- 1105 spiral matrix (25 points)
- Technology sharing | anyrtc service single port design
- Summary of C language interview questions
- Kubernetes: a comprehensive analysis of container choreography
- J - Borg maze (minimum spanning tree +bfs)
- Steps for commissioning of vertical machining center
- 4.5 integer
- 1082 read number in Chinese (25 points)
- 1148 werewolf - Simple Version (20 points)
- Teach you a learning method to quickly master knowledge
猜你喜欢
Some reference routines for cache update
Repair of incorrect deletion of win10 boot entry
CCF date calculation (Full Score code + skill summary) February 2, 2015
Rte2021 review of the practice and the way of AI OPS landing
Chapter 2 installation and use of vscode editor
The principle of fluent 2 rendering and how to realize video rendering
[matlab] 3D drawing summary
[matlab] 2D drawing summary
NPM install --global --save --save dev differences
Bye civil engineering, hello CS, can you change the certificate to the Blue Bridge Cup
随机推荐
Matlab draws the image of the larger function value of the two functions (super simple)
4.4 string
[untitled]
Is Domain Driven Design (DDD) reliable?
Forward declaration of classes
IO interview questions
高清机械原理 · 机械设计经典动图
M - smooth engineering continuation (minimum spanning tree)
Analysis on the problems of irregular step hole on horizontal machining center
DR-TANet: Dynamic Receptive Temporal Attention Network for Street Scene Change Detection
Bucket sorting (C language)
1025 pat ranking (25 points)
Scattered knowledge of C language (unfinished)
Chapter III installation and use of jupyter
4.7 type() function query data type
Database connection to company database denied
4.3 variables and assignments
L - Jungle roads (minimum spanning tree)
Text matching - [naacl 2022] GPL
1019 general palindromic number (20 points)