当前位置:网站首页>深入理解.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();
}
}
效果如下
总结
可以看出加上了事件锁后,确实能保证输出的顺序,而没有加锁的数据是乱序,甚至可能出行脏数据的情况
边栏推荐
- CCF elimination games (Full Score code + problem solving ideas + skill summary) February 2, 2015
- Analysis on the problems of irregular step hole on horizontal machining center
- Experiment of the planning group of the West University of technology -- pipeline CPU and data processing Adventure
- G - navigation nightare
- Guada digital analog
- 001 data type [basic]
- Infrastructure is code. What are you talking about?
- 4.3 variables and assignments
- 1018 public bike Management (30 points)
- return statement
猜你喜欢

Review 2021, embrace change and live up to Shaohua

Svn password forgetting solution

Kubernetes: a comprehensive analysis of container choreography

(Niuke) BFS

Chapter 2 installation and use of vscode editor

Determine the number of digits of an integer in MATLAB (one line of code)

Webrtc: industrial application based on Internet of things

4.3 variables and assignments

Win10 one click Reset win10 to solve all system bugs without deleting any files and Applications

Summary of system stability construction practice
随机推荐
M - smooth engineering continuation (minimum spanning tree)
CCF date calculation (Full Score code + skill summary) February 2, 2015
Imitating freecodecamp to build a programming ability test platform
分布式--OpenResty+lua+Redis
Summary of system stability construction practice
[matlab] 2D drawing summary
[matlab] 3D drawing summary
Technology sharing | how to quickly realize audio and video online calls
Single cycle CPU of the design group of West University of Technology
Chapter III installation and use of jupyter
Zero basic C language learning notes -- first introduction -- 2 data types & variables and constants
1025 pat ranking (25 points)
How many questions can you answer for the interview of Mechanical Engineer?
How does sd-rtn ensure the high availability of RTE services after infrastructure failure
Xiao Sha's pain (thinking problem)
At the beginning of the 2022 new year, I will send you hundreds of dry articles
How should we understand the variability of architecture design?
J - Borg maze (minimum spanning tree +bfs)
Machine learning feature selection
day02