当前位置:网站首页>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编程问题,测试用例不通过
- JS converts an array to a two-dimensional array based on the same value
- [kali] Kali system, software update (with image source)
- 【系统分析师之路】第五章 复盘软件工程(软件过程改进)
- 点击table的td单元格出现dialog弹窗,获取值后将值放回td单元格
- (8)JMeter元件详解之 Once only Controller 仅一次控制器
- Resource realization applet opening wechat official small store tutorial
- 一次 Keepalived 高可用的事故,让我重学了一遍它!
- numpy 创建空数组 data = np.empty(shape=[1, 64,64,3])
- “即服务”,企业数字化转型的必然选择
猜你喜欢

Goods and services - platform properties

【刷题篇】爱吃香蕉的珂珂

This editor will open source soon!

Today's sleep quality record 80 points

Waving flags and shouting for basic language

This article explains the concepts of typed array, arraybuffer, typedarray, DataView, etc

Assertions of regular series

SQL考勤统计月报表

WTM major updates, multi tenancy and single sign on

为基础性语言摇旗呐喊
随机推荐
今日睡眠质量记录80分
Today's sleep quality record 80 points
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)
Goods and services - platform properties
SQL programming problem, test case failed
Click the TD cell of table to open the dialog pop-up window. After obtaining the value, put the value back into the TD cell
为基础性语言摇旗呐喊
QQ 居然被盗了?原因在这......
Basic syntax of unity script (1) - common operations of game objects
Unity Animator 参数
正则系列之断言Assertions
How does MySQL merge columns?
60 divine vs Code plug-ins!!
[Title brushing] coco, who likes bananas
[the path of system analyst] Chapter 5 Software Engineering (Agile Development)
防火墙基础之总部双机热备与分支基础配置
【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
表格储存中sql查询的时候,查询结果增加主键报错,查询结果超过10w行。需要对主键增加上多元索引吗?
知识传播不能取代专业学习!