当前位置:网站首页>Deep understanding Net (2) kernel mode 2 Kernel mode construct semaphone
Deep understanding Net (2) kernel mode 2 Kernel mode construct semaphone
2022-06-30 13:49:00 【Cucumbers need programming】
In depth understanding of .Net The construction mode of thread synchronization in ( Two ) Kernel mode construction
Preface
Kernel-mode Constructs Generally translated as kernel mode construction ,
Constructs It is both a noun and a verb , But the translation for construction feels a bit like a verb , I think it's better to translate it into a noun , After all s Of , It means multiple , Verbs obviously can't express more than one , Such as kernel mode constructs , Kernel mode constructs .
Environmental statement
IDE:Visual Studio 2022
OS:Win10
.NET:.Net4.6.1
One 、 What is the semaphore ?
What is the semaphore ? I remember when the teacher said "yes" 0,1 To control thread blocking and unblocking is a semaphore . This is the most basic understanding of semaphores , Semaphores are atomic and controlled by the system kernel int Variable , by 0 Blocking threads when , Greater than 0 Remove the obstruction when you need to .
Two 、 Code writing
1. Write a Semaphone Lock of
The code is as follows ( Example ):
public class SemaphoreTest
{
public void TestStart()
{
Console.WriteLine(" SemaphoreTest TestStart Start !");
IntObj intObj = new IntObj();
for (Int32 i = 0; i < 10; i++)
{
new TaskFactory().StartNew(() =>
{
AddCount(intObj);
AddCount_SemaphoreLock(intObj);
});
}
Console.WriteLine(" SemaphoreTest TestStart END !");
}
public void AddCount(IntObj intObj)
{
intObj.num_Norml++;
WriteLineThread(intObj);
Thread.Sleep(500);
}
SemaphoreLock tLock = new SemaphoreLock(1);
public void AddCount_SemaphoreLock(IntObj intObj)
{
tLock.Enter();
intObj.num_Lock++;
WriteLineThread_SemaphoreLock(intObj);
Thread.Sleep(500);
tLock.Leave();
tLock.Leave();
}
public void WriteLineThread(IntObj intObj )
{
Thread.Sleep(200);
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"num_Norml :{
intObj.num_Norml},{
Thread.CurrentThread.ManagedThreadId}");
Console.ForegroundColor = currentForeColor;
Thread.Sleep(200);
}
public void WriteLineThread_SemaphoreLock(IntObj intObj)
{
Thread.Sleep(200);
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"num_Lock:{
intObj.num_Lock},{
Thread.CurrentThread.ManagedThreadId}");
Console.ForegroundColor = currentForeColor;
Thread.Sleep(200);
}
}
2. test Semaphone Lock effect
The code is as follows ( Example ):
public class SemaphoreTest
{
public void TestStart()
{
Console.WriteLine(" SemaphoreTest TestStart Start !");
IntObj intObj = new IntObj();
for (Int32 i = 0; i < 10; i++)
{
new TaskFactory().StartNew(() =>
{
AddCount(intObj);
AddCount_EventLock(intObj);
});
}
Console.WriteLine(" SemaphoreTest TestStart END !");
}
public void AddCount(IntObj intObj)
{
intObj.num_Norml++;
WriteLineThread(intObj);
Thread.Sleep(500);
}
SemaphoreLock tLock = new SemaphoreLock(10);
public void AddCount_EventLock(IntObj intObj)
{
tLock.Enter();
intObj.num_Lock++;
WriteLineThread_EventLock(intObj);
Thread.Sleep(500);
tLock.Leave();
}
public void WriteLineThread(IntObj intObj )
{
Thread.Sleep(200);
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"num_Norml :{
intObj.num_Norml},{
Thread.CurrentThread.ManagedThreadId}");
Console.ForegroundColor = currentForeColor;
Thread.Sleep(200);
}
public void WriteLineThread_EventLock(IntObj intObj)
{
Thread.Sleep(200);
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"num_Lock:{
intObj.num_Lock},{
Thread.CurrentThread.ManagedThreadId}");
Console.ForegroundColor = currentForeColor;
Thread.Sleep(200);
}
}
The effect is as follows
When the semaphore lock is initialized, the maximum is allowed 10 Thread effects
When the semaphore lock is initialized, the maximum is allowed 1 Thread effects
The result at this point is the same as AutoResetEvent Event lock has the same effect
summary
It can be seen that , When the maximum number of threads 1 When , Semaphores and AutoResetEvent The effect of the event is the same . However, if the semaphore is released multiple times, it may occur SemaphoreFullException It's abnormal .
When the maximum number of threads is greater than 1 When , It will be possible to have multiple threads simultaneously variable . In that case ?
边栏推荐
- SQL attendance statistics monthly report
- Rk356x u-boot Institute (command section) 3.2 usage of help command
- 香港回归20余年,图扑数字孪生港珠澳大桥,超震撼
- mysql拒绝访问、管理员身份打开的
- 今日睡眠质量记录80分
- Simple understanding of the difference between get request and post submission
- ABAP工具箱 V1.0(附实现思路)
- Directory related commands
- Common UI components
- Basic syntax of unity script (1) - common operations of game objects
猜你喜欢
编程实战赛来啦!B站周边、高级会员等好礼送你啦!
Observable, seulement fiable: première bombe de salon de la série cloudops d'exploitation et d'entretien automatisés dans le nuage
【C】 In depth understanding of pointers and callback functions (Introduction to simulating qsort)
DeFi“钱从哪来”?一个大多数人都没搞清楚的问题
Rk356x u-boot Institute (command section) 3.3 env related command usage
kaniko官方文档 - Build Images In Kubernetes
科普达人丨漫画图解什么是eRDMA?
Observable, reliable: the first shot of cloudops series Salon of cloud automation operation and maintenance
【刷题篇】避免洪水泛滥
Apache Doris Compaction優化百科全書
随机推荐
PG Basics - logical structure management (table inheritance, partition table)
Waving flags and shouting for basic language
In the digital age, XDR (extended detection and response) has unlimited possibilities
一篇文章读懂关于企业IM的所有知识点
一文讲清楚什么是类型化数组、ArrayBuffer、TypedArray、DataView等概念
【科研数据处理】[实践]类别变量频数分析图表、数值变量分布图表与正态性检验(包含对数正态)
Directory related commands
[deep anatomy of C language] storage principle of float variable in memory & comparison between pointer variable and "zero value"
ABAP toolbox v1.0 (with implementation ideas)
golang模板(text/template)
【系统分析师之路】第五章 复盘软件工程(敏捷开发)
Apache Doris Compaction優化百科全書
编程实战赛来啦!B站周边、高级会员等好礼送你啦!
Jetpack Compose 实现完美屏幕适配
Basic syntax of unity script (2) -record time in unity
(8)JMeter元件详解之 Once only Controller 仅一次控制器
Multi terminal collaboration of Huawei accounts to create a better internet life
VisualStudio and SQL
Methodology for troubleshooting problems (applicable to troubleshooting problems arising from any multi-party cooperation)
Kaniko official documents - build images in kubernetes