当前位置:网站首页>深入理解.Net中的线程同步之构造模式(二)内核模式2.内核模式构造物Semaphone
深入理解.Net中的线程同步之构造模式(二)内核模式2.内核模式构造物Semaphone
2022-06-30 13:10:00 【小黄瓜要编程】
深入理解.Net中的线程同步之构造模式(二)内核模式构造
前言
Kernel-mode Constructs一般翻译为内核模式构造 ,
Constructs 即是一个名词也是一个动词,但是翻译为构造感觉有点像一个动词,个人感觉翻译为名词更好,毕竟是加了s的,就表示多个,动词显然不能表示多个啊,比如内核模式构造物,内核模式构造体。
环境说明
IDE:Visual Studio 2022
OS:Win10
.NET:.Net4.6.1
一、信号量是什么?
信号量是什么?记得嵌入式的时候老师说过通过0,1来控制线程的阻断和解除阻断就是信号量。这个就是信号量最基础的理解了,信号量其实是由系统内核控制的具有原子性的int变量,为0时阻塞线程,大于0时解除阻塞。
二、代码编写
1.编写一个Semaphone的锁
代码如下(示例):
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.测试Semaphone锁的效果
代码如下(示例):
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);
}
}
效果如下
当信号量锁初始化的时候允许最大10个线程效果
当信号量锁初始化的时候允许最大1个线程效果
此时的结果就和AutoResetEvent事件锁一样的效果了
总结
可以看出,当最大线程数1的时候,信号量和AutoResetEvent事件效果一样。但是信号量的如果多次释放可能会出现SemaphoreFullException的异常。
当最大线程数大于1的时候,将可能有多个线程同时变量。那种情况使用呢?
边栏推荐
- 随着产业互联网的发展,有关互联网的落地和应用也就变得宽阔了起来
- Basic syntax of unity script (5) - vector
- PG基础篇--逻辑结构管理(表继承、分区表)
- Hangzhou E-Commerce Research Institute: the official website (website) is the only form of private domain
- 【科学文献计量】外文文献及中文文献关键词的挖掘与可视化
- 一篇文章读懂关于企业IM的所有知识点
- [Select] resource realization information, news, we media, blog applet (can be drained, open traffic master, with PC background management)
- navicat数据库建表是没有utf8选项。
- In line with the trend of media integration, Zhongke Wenge and Meishe jointly create digital intelligence media publicity
- exlipse同时操作多行。比如同时在多行同列输入相同的文字
猜你喜欢
![[Title brushing] avoid flooding](/img/2d/95498d54c0c3c5ca79bb72b7a977bc.png)
[Title brushing] avoid flooding

逆向调试入门-PE中的VA与RVA换算04/07

Hangzhou E-Commerce Research Institute: the official website (website) is the only form of private domain

腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?

How can I protect my private key?

A keepalived high availability accident made me learn it again!

DeFi“钱从哪来”?一个大多数人都没搞清楚的问题

Embedded development: five C features that may no longer be prohibited

香港回归20余年,图扑数字孪生港珠澳大桥,超震撼

Unity Animator 参数
随机推荐
Read all the knowledge points about enterprise im in one article
服务线上治理
点击table的td单元格出现dialog弹窗,获取值后将值放回td单元格
How can c write an SQL parser
Common UI components
JMeter learning notes
数据库表为什么写不进数据了
Methodology for troubleshooting problems (applicable to troubleshooting problems arising from any multi-party cooperation)
Data Lake (11): Iceberg table data organization and query
数字化转型道阻且长,如何迈好关键的第一步
golang文件的写入、追加、读取、复制操作:bufio包的使用示例
golang模板(text/template)
【科学文献计量】外文文献及中文文献关键词的挖掘与可视化
[recruitment (Guangzhou)] Chenggong Yi (Guangzhou) Net core middle and Senior Development Engineer
STM32 porting the fish component of RT thread Standard Edition
Apache Doris Compaction优化百科全书
今日睡眠质量记录80分
[Title brushing] coco, who likes bananas
【招聘(广州)】成功易(广州).Net Core中高级开发工程师
rxjs Observable 两大类操作符简介