当前位置:网站首页>深入理解.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();
}
}
效果如下
总结
可以看出加上了事件锁后,确实能保证输出的顺序,而没有加锁的数据是乱序,甚至可能出行脏数据的情况
边栏推荐
- How should we understand the variability of architecture design?
- 1035 password (20 points)
- Single cycle CPU of the design group of West University of Technology
- Machine learning feature selection
- Summary of C language interview questions
- Bye civil engineering, hello CS, can you change the certificate to the Blue Bridge Cup
- Basic literacy - four common software architectures
- My own opinion on lisp
- 1027 colors in Mars (20 points)
- Abstract meaning
猜你喜欢

The short video and live broadcast incubation training camp with goods opens nationwide enrollment!

Experiment of the planning group of the West University of technology -- pipeline CPU and data processing Adventure

Complement (Niuke)

4.12 input() input function and comments

How to get palindrome number in MATLAB (using fliplr function)

RTC monthly tabloid programming challenge ended successfully in June; Review of the first anniversary of sound network's listing

Matlab construction operation example

How does sd-rtn ensure the high availability of RTE services after infrastructure failure

CCF elimination games (Full Score code + problem solving ideas + skill summary) February 2, 2015

Rte2021 review HDR technology product practice and exploration
随机推荐
Text matching - [naacl 2021] augsbert
I - constructing roads
IO interview questions
Bye civil engineering, hello CS, can you change the certificate to the Blue Bridge Cup
1062 talent and virtue (25 points)
The short video and live broadcast incubation training camp with goods opens nationwide enrollment!
Tetris source code (color version)
Repair of incorrect deletion of win10 boot entry
Why do high precision CNC machining centers have errors? You should pay attention to these four reasons!
Is Domain Driven Design (DDD) reliable?
Webrtc: industrial application based on Internet of things
1131: genetic correlation
Three types of technical debt that programmers often encounter: code, data, and architecture
How to get palindrome number in MATLAB (using fliplr function)
4.12 input() input function and comments
L - Jungle roads (minimum spanning tree)
Rte2021 review HDR technology product practice and exploration
FoxPro and I
Soap comparison result file description
Bucket sorting (C language)