当前位置:网站首页>[interview: concurrent article 28:volatile] orderliness
[interview: concurrent article 28:volatile] orderliness
2022-07-28 01:52:00 【I cream】
【 interview : Concurrent articles 28:volatile】 Orderliness
00. Preface
If you have any questions, please point out , thank .
01. Introduce
What is order
Without changing the results of the program Instructions can be reordered and combined at various stages to achieve instruction level parallelism .
Be careful :
Reorder instructions cannot affect the results
Why do you do this
cpu Treatment process
stay cpu Processing an instruction involves a series of processes , Contains : Fingering decoding Address generation perform Write back , These five steps But these five steps can be staggered , That is, when one instruction is decoded, another instruction can take a finger .
Example 1
x=a;
y=b;

Such implementation efficiency is obviously faster , And when there is no relationship between statements The order of execution can be arbitrary , That is, it is possible to execute y=b Re execution x=a, It doesn't affect the result
Example 2
x=a;
c=x+1;
y=b;
Whether this code can apply the above picture , You can't .
Theoretically, it should correspond to the following figure 
But in fact, this code may be optimized
x=a;
y=b;
c=x+1;
The corresponding picture 
And the reason for that is c=x+1 need x The value of That is, we must wait x Can only be executed after all the instructions of c Instructions , But in the original order y=b It's at the end of That is, he also needs to be in x Instructions can only be run after execution however y=b It has nothing to do with the first two instructions , So optimized Make in x Operation period y It can also run , Improved concurrency , Reduce the situation of running a single instruction .
summary : This case in example 2 is instruction reordering , In the case of single thread, there is no impact , But in the case of multithreading, there may be problems .
02. Instruction sequencing in multithreading
public class Disorder {
private static int x=0,y=0;
private static int a=0,b=0;
// No matter how the two threads are arranged and combined If there is no reordering , It must not exist x=0 And y=0
public static void main(String[] args) throws InterruptedException {
int i=0;
// Endless loop
while (true){
i++;
x=0;y=0;
a=0;b=0;
Thread one= new Thread(()-> {
// There is no dependency a=1 and x=b
a=1;
x=b;
});
Thread other = new Thread(()-> {
// If there is no disorder ,b=1 It must be y=a front
b=1;
y=a;
});
one.start();other.start();
one.join();other.join();
String result = " The first "+i+" Time ("+x+","+y+")";
if(x==0 && y==0){
System.err.println(result);
break;
}else{
System.out.println(result);
}
}
}
public static void shortWait(long interval){
long start = System.nanoTime();
long end;
do{
end=System.nanoTime();
}while (start+interval>=end);
}
}
result
.
.
.
.
The first 181799 Time (0,1)
The first 181800 Time (0,1)
The first 181801 Time (0,1)
The first 181802 Time (0,1)
The first 181803 Time (0,1)
The first 181804 Time (0,0)
explain
We analyze the above code , Consider multithreading , Theoretically x,y There is only 3 Kind of (1,0) (1,1) (0,1), But we found the fourth situation (0,0) It must be x=b stay a=1 Before ,y=a stay b=1 Before , Obviously, in this case, there is only instruction reordering
03. Disable instruction reordering
It's very simple. It's in x,y Add one before volatile, The function is to ensure that x,y The previous code executes before it , for example a=1;x=b; Guarantee a=1 It must be x=b Before execution , Empathy y In the same way , At the same time, the previous variables, including their own values, are synchronized to main memory . At the same time, we need to give a,b with volatile Guarantee a b The visibility of Otherwise, there may be a b Has been updated to main memory But what is read is still local memory
Add : The last paragraph of explanation may not be understood clearly , The specific explanation involves volatile Principle , The next article will talk about .
边栏推荐
猜你喜欢

抓包精灵NetCapture APP抓包教程《齐全》

Qt 绘制系统简介

Understand shader

Leetcode: 515. Find the maximum value in each tree row

If you are still using WiFi, you will be out: li-fi is better!!!

Can ordinary equipment access TSN time sensitive network?

Fiddler 手机抓包代理设置(针对华为荣耀60S)

Docker builds MySQL master-slave locally

文章复现:超分辨率网络FSRCNN

机器学习如何做到疫情可视化——疫情数据分析与预测实战
随机推荐
喜欢听的歌曲
Leetcode 2351. the first letter that appears twice
周报、月报有多折磨人?万能报表模板建议收藏!(附模板)
Qlib tutorial - based on source code (II) local data saving and loading
领域驱动设计——术语篇
C language · pointer
Recursion related exercises
抓包精灵NetCapture APP抓包教程《齐全》
Summary: Prometheus storage
Stock problems 5 times
HCIP第十二天笔记
以“数字化渠道”撬动家用电器消费蓝海,经销商在线系统让企业生意更进一步
QT setting Icon
自定义事件
在生产型企业中,MES系统有哪些重要应用
Thinking about some things
Leetcode 2341. How many pairs can an array form
Favorite songs
三舅的故事
26. Abstraction and template ideas