当前位置:网站首页>I haven't delivered books for a long time, and I feel uncomfortable all over
I haven't delivered books for a long time, and I feel uncomfortable all over
2022-07-27 09:38:00 【AI Xiaoxian】
If you don't want to read the article, just pull it to the end to participate in the lottery
What is instruction reordering ?
In order to understand more intuitively , I still use a case to illustrate .
public class MemoryReorderingExample {
private static int x=0,y=0;
private static int a=0,b=0;
public static void main(String[] args) throws InterruptedException {
int i=0;
while(true){
i++;
x=0;y=0;
a=0;b=0;
Thread t1=new Thread(()->{
a=1;
x=b;
});
Thread t2=new Thread(()->{
b=1;
y=a;
});
t1.start();
t2.start();
t1.join();
t2.join();
String result=" The first "+i+" Time ("+x+","+y+")";
if(x==0&&y==0){
System.out.println(result);
break;
}
}
}
}The logic of the above program is as follows :
Define four int Variable of type , Initialization is 0.
Define two threads t1、t2,t1 Thread modification a and x Value ,t2 Thread modification b and y Value , Start two threads separately .
Under normal circumstances ,x and y Value , Will be based on t1 and t2 The execution of the thread .
If t1 Thread priority , So the result is x=0、y=1.
If t2 Thread priority , So the result is x=1、y=0.
If t1 and t2 Thread executing at the same time , So the result is x=1、y=1.
Let's take a look at the results :
The first 136164 Time (0,0)Readers are not surprised to see this result ? Running 13 After 10000 times , I got one x=0、y=0 Result .
In fact, this is the so-called instruction reordering problem , Suppose the above code is reordered by instructions , Become the following structure :
Thread t1=new Thread(()->{
x=b; // Instruction reordering
a=1;
});
Thread t2=new Thread(()->{
y=a; // Instruction reordering
b=1;
});After reordering , If t1 and t2 Thread running at the same time , Will get x=0、y=0 Result , The result is from a human perspective , It's kind of like t1 In the thread a=1 The modification result of t2 Threads are not visible , Again t2 In the thread b=1 The result of the implementation of t1 Threads are not visible .
01
What is instruction reordering
Instruction reordering Refers to the compiler or CPU A means of reordering instructions to optimize the execution performance of a program , Reordering causes visibility problems , Therefore, in multi-threaded development, we must pay attention to and avoid reordering .
From the source code to the final running instructions , It will go through the following two stages of reordering .
The first stage , Compiler reorder , Is in the compilation process , The compiler reorders instructions according to context analysis , The aim is to reduce CPU Interaction with memory , After reordering, try to ensure that CPU Read data from registers or cache lines .
In front JIT The loop expression mentioned in the optimization (Loop Expression Hoisting) It's compiler level reordering , from CPU On the other hand , It avoids the processor from loading in memory every time stop, Reduce the interaction overhead between processor and memory .
if(!stop){
while(true){
i++;
}
}The second stage , Processor reorder , The processor reordering is divided into two parts .
Parallel instruction set reordering , This is a form of processor optimization , The processor can change the execution order of instructions .
Memory system reorder , This is the introduction of the processor Store Buffer The instruction execution sequence caused by buffer delay writing is inconsistent , It will be explained in detail in the following content .
To help readers understand , The author makes a simple explanation for the principle of parallel instruction set .
What is a parallel instruction set ?
There are usually multiple execution units in the processor core , For example, arithmetic logic unit 、 Displacement element, etc . Before introducing parallel instruction sets ,CPU Only a single instruction can be executed in each clock cycle , That is, only one execution unit is working , Other execution units are idle ; After introducing the parallel instruction set ,CPU In one clock cycle, multiple instructions can be allocated to execute in different execution units at the same time .
So what is the reordering of parallel instruction sets ?
As shown in the figure below , Suppose a program has multiple instructions , The execution and implementation of different instructions are also different .

chart Parallel instruction set reordering
For an instruction that reads data from memory ,CPU One of the execution units executes the instruction and waits until the result is returned , according to CPU It's fast enough to handle hundreds of other instructions , and CPU In order to improve the efficiency of execution , It will be analyzed according to the idle state of the unit circuit and whether the instructions can be executed in advance , Advance the instructions with the last instruction address order before reading the memory instructions .
actually , The essence of this optimization is to fill the gap by executing other executable instructions in advance CPU Time gap , Then reorder the results at the end , So as to realize the running result of sequential execution of instructions .
02
as-if-serial semantics
as-if-serial Indicates that all program instructions can be reordered due to optimization , However, in the optimization process, we must ensure that it is in a single threaded environment , The running result after reordering is consistent with the expected execution result of the program code itself ,Java compiler 、CPU The reordering of instructions needs to be guaranteed in a single threaded environment as-if-serial The semantics are correct .
Some readers may have doubts , Since it can ensure the order in a single threaded environment , So why is there instruction reordering ? stay JSR-133 Specification , This is what the original text says .
The compiler, runtime, and hardware are supposed to conspire to create the illusion of as-if-serial semantics, which means that in a single-threaded program, the program should not be able to observe the effects of reorderings.However, reorderings can come into play in incorrectly synchronized multithreaded programs, where one thread is able to observe the effects of other threads, and may be able to detect that variable accesses become visible to other threads in a different order than executed or specified in the program.
as-if-serial Semantics allow reordering ,CPU Instruction optimization at the level still exists . In a single thread , These optimizations do not affect the overall execution results , In multithreading , Reordering causes visibility problems .
in addition , In order to ensure as-if-serial The semantics are correct , Compilers and processors do not reorder instructions for dependent operations , Because this will affect the execution result of the program . Let's look at the following code .
public void execute(){
int x=10; //1
int y=5; //2
int c=x+y; //3
}The above code should be... In the normal execution order 1、2、3, In multithreaded environment , There may be 2、1、3 Such execution order , But it won't happen 3、2、1 In this order , because 3 And 1 and 2 There is a data dependency , Once reordered , There's no guarantee as-if-serial The semantics are correct .
thus , I believe readers have a basic understanding of the visibility problem caused by instruction reordering , But in CPU There is also a memory system reordering problem at the level , Memory system reordering can also cause visibility problems ,《Java Deep analysis and practice of concurrent programming 》 The book will also make a detailed analysis around this problem .
* This article is excerpted from 《Java Deep analysis and practice of concurrent programming 》 A Book , Welcome to read this book for more wonderful content !


▊《Java Deep analysis and practice of concurrent programming 》
Tan Feng Writing
Java A masterpiece of concurrent programming
13 year Java Development and architecture experience +4 Summary of in-depth research on concurrent programming in
cover Java The core library and core classes of the whole concurrent programming system
A large number of design ideas are combined with practical cases
This book covers Java The use and principle analysis of the core library and core classes of concurrent programming system , Including threads 、synchronized、volatile、J.U.C Reentry lock and read-write lock in 、 Conditional waiting mechanism in concurrency 、J.U.C Concurrent toolset 、 Explore the tools you have to know about concurrent programming 、 Blocking queues 、 Concurrent security collection 、 Thread pool 、 Asynchronous programming features, etc . For every technical point in the book , Longitudinal analysis of everything related to it , And the relevant knowledge points are described in great detail , At the same time, we look at concurrency from the perspective of architecture practice , Through a large number of practical cases, readers can understand the application methods of various technologies in practical application .
The next step is the book delivery ~,10 Ben 《Java Deep analysis and practice of concurrent programming 》 Send 10 Lucky little friend , Identify QR code or reply in the background of official account 1207 Get lucky draw link ~

边栏推荐
猜你喜欢

XML概述

电机控制器中的MOS驱动

C language takes you to tear up the address book

【云原生 • DevOps】一文掌握容器管理工具 Rancher

【微信小程序】农历公历互相转换

给自己写一个年终总结,新年快乐!
![[Wuhan University of technology] information sharing for the preliminary and second examinations of postgraduate entrance examination](/img/15/298ea6f7367741e1e085007c498e51.jpg)
[Wuhan University of technology] information sharing for the preliminary and second examinations of postgraduate entrance examination

Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?

Sentinel 万字教程 | 文末送书

好久不送书,浑身不舒服
随机推荐
如何在树莓派上安装cpolar内网穿透
vscode使用remote-ssh连接以及连接失败的解决方法
【微信小程序】农历公历互相转换
七月集训(第12天) —— 链表
年底了,我教你怎么拿高绩效!
BGP联邦实验
swagger-editor
会议OA项目之会议排座功能&&会议送审的实现
Nine ways to read the file path under the resources directory
深入浅出详解Knative云函数框架!
命令提示符启动不了mysql,提示发生系统错误 5。拒绝访问。解决办法
工程测量模拟A卷
七月集训(第13天) —— 双向链表
为什么微服务一定要有API网关?
NCCL 集合通信--Collective Operations
七月集训(第17天) —— 广度优先搜索
[raspberry pie] box related manual-4 web agent
给自己写一个年终总结,新年快乐!
35-Spark Streaming反压机制、Spark的数据倾斜的解决和Kylin的简单介绍
《工程测量学》考试复习总结