当前位置:网站首页>In depth understanding Construction mode of thread synchronization in. Net (1) user mode construction
In depth understanding Construction mode of thread synchronization in. Net (1) user mode construction
2022-04-22 01:43:00 【Cucumbers need programming】
- Two construction modes
There are two primitive constructions : User mode (user-mode) And kernel mode ( kernel-mode). Primitive user patterns should be used as much as possible to construct , They are significantly faster than kernel mode construction . Why is user mode faster than kernel mode ? This is because they use special CPU Instructions to coordinate threads . This means that coordination takes place in hardware ( That's why it's so fast ).
In other words, user mode is not blocked, but blocked for a short time , At the hardware level , For kernel systems , He didn't know there was a blockage , All think it will never block . The kernel mode is a virtual structure that the system helps us , Compared with the user structure, it has poor performance , It will block but not occupy cpu Keep running . Like us while(true) when , Will let the thread sleep for a while , To let other threads use cpu - this 2 Each construction mode has its advantages and disadvantages , Combine them , The third mode of travel , Mixed structure hybrid construct, Hybrid construction can be called quickly without wasting cpu Calculation power .
-
window User mode construction under
- Variable structure
Variable construction means that the operation of a data is non atomic , Data may be stuck in cpu In cache , Thus causing dirty reading and writing of data ( According to the dirty reading of the database ), How to change the structure ?
Use the variable volatile modification , It can guarantee the atomicity of variables
volatile Can be modified Boolean,(S)Byte,(U)Int16,(U)Int32,(U)IntPtr, Single and Char type
public volatile int int_a;
public volatile char char_a;
Use volatile Please note that , Don't abuse volatile, It may cause the compiler to give up optimizing part of the code and cause performance loss .
When It may cause some performance loss , Select optimized code at compile time , The preferred 32 Bit system
internal class vol_Test
{
public volatile int int_a;
public volatile char char_a;
public int int_b;
public int int_c;
public void TestStart()
{
for (int i = 0; i < 10; i++) {
{
B_Add();
A_Add();
}
}
}
public void A_Add()
{
System.Diagnostics.Stopwatch oTime = new System.Diagnostics.Stopwatch(); // Define a timing object
oTime.Start(); // Start timing
for (int i = 0; i < 100000000; i++)
{
int_a = 1;
for (int j = 0; j < 30; j++)
{
int_a +=int_a;
}
}
oTime.Stop();
Console.WriteLine($"AAAAAAAAAAAAA A_Add {
int_a} The running time of the program :{
oTime.Elapsed.TotalSeconds} second {
oTime.Elapsed.TotalMilliseconds} millisecond ");
}
public void B_Add()
{
System.Diagnostics.Stopwatch oTime = new System.Diagnostics.Stopwatch(); // Define a timing object
oTime.Start(); // Start timing
for (int i = 0; i < 100000000; i++)
{
int_b = 1;
for (int j = 0; j < 30; j++)
{
int_b += int_b;
}
}
oTime.Stop();
Console.WriteLine($"B_Add {
int_b} The running time of the program :{
oTime.Elapsed.TotalSeconds} second {
oTime.Elapsed.TotalMilliseconds} millisecond ");
}
}
Running results
It's hard to see volatile The difference between variables and ordinary variables , It can only be said that in theory volatile The performance of variables may be slightly inferior to ordinary variables

- Interlock construction
The interlocking structure is mainly for Interlocked Use ,Interlocked The methods in the class are atomic

in other words ,Interlocked add perhaps read One int Data time , There must be no dirty reading
utilize Interlocked Characteristics of , We can implement a simple spin lock
public struct SimpleSpinLock{
private int m_Lock;
public void Enter(){
while (true){
if (Interlocked.Exchange(ref m_Lock, 1) == 0) return;
}
}
public void Leave(){
Volatile.Write(ref m_Lock, 0);
}
}
// Here's how to use SimpleSpinLock Example
public class Simple{
private SimpleSpinLock m_lock = new SimpleSpinLock();
public void AccessResource(){
m_lock.Enter();
// Perform certain procedures , Only one thread can enter here
m_lock.Leave();
}
}
Event code , Using spin lock in this way, although the performance is very good , But it will take up a lot of cpu Time , therefore , Spin alone is not enough .
版权声明
本文为[Cucumbers need programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220141086870.html
边栏推荐
猜你喜欢

DVWA series - XSS (cross site script injection (reflective, storage, DOM))

The playback volume exceeds 2000W. Why can these high-energy qiafan videos be "watched repeatedly" by users of station B?

2022年ADB 命令知多少?一文2000字详细 ADB 命令大全来啦

PHY6222国产低功耗多协议蓝牙SoC芯片

C# UserControl造成内存泄漏
阿里大牛整理,android面试选择题

Automatic multiorgan segmentation in thorax CT images using U-net-GAN

点云分割(point cloud segmentation)任务笔记

VMware virtual machine installation detailed tutorial network NAT and bridge configuration

Isla Obscura 深海生物系列 NFT 上架市场平台
随机推荐
B站充电|单周充电超1500,银发UP主成B站新黑马?
Compiled vs interpreted, dynamic vs static
【学会放下
MySQL realizes master-slave replication / master-slave synchronization
Code source daily question div1 (101-109)
How to call the login, playback address and video playback interface of easycvr platform?
代码源每日一题 div1 (301-307)
Can SQL server remotely manipulate a set procedure or function and return results without using account login?
As a software testing practitioner, do you understand your development direction?
Point cloud segmentation task notes
net easy
Isla obscura deep sea biological series NFT market platform
MATLAB实现多元正态Copula分布
Thales
Mr robot靶机渗透测试
Installation package signature detection
MSF series - Introduction and use of basic commands
pycharm实现设置自动的参数注释标识
Code source daily question div1 (201-207)
Rotation formula of two-dimensional vector