当前位置:网站首页>Instruction rearrangement under multithreading concurrency
Instruction rearrangement under multithreading concurrency
2022-07-29 06:47:00 【The evil way does not miss the firewood cutting skill】
In the case of multi-threaded concurrency , Instruction rearrangement will cause problems , Let's take a look at the following example :
public class DemoCount {
public static int r1, r2, x, y;
public static void main(String[] args) throws InterruptedException {
boolean f1, f2, f3, f4;
f1 = f2 = f3 = f4 = false;
int count = 0;
while (true) {
r1 = r2 = x = y = 0;
Thread t1 = new Thread(() -> {
x = 1;
r1 = y;
});
Thread t2 = new Thread(() -> {
y = 1;
r2 = x;
});
t1.start();
t2.start();
t1.join();// Ensure threads 0、1 Can execute main The thread prints the following output
t2.join();// Ensure threads 0、1 Can execute main The thread prints the following output
++count;
if (r1 == 0 & r2 == 1 & !f1) {
System.out.println(" The first " + count + " Occurrence results : r1=" + r1 + ":r2=" + r2);
f1 = true;
}
if (r1 == 1 & r2 == 0 & !f2) {
System.out.println(" The first " + count + " Occurrence results : r1=" + r1 + ":r2=" + r2);
f2 = true;
}
if (r1 == 0 & r2 == 0 & !f3) {
System.out.println(" The first " + count + " Occurrence results : r1=" + r1 + ":r2=" + r2);
f3 = true;
}
if (r1 == 1 & r2 == 1 & !f4) {
System.out.println(" The first " + count + " Occurrence results : r1=" + r1 + ":r2=" + r2);
f4 = true;
}
}
}
}
Running will produce four results :
The first 1 Occurrence results : r1=0:r2=1
The first 59 Occurrence results : r1=1:r2=0
The first 359000 Occurrence results : r1=1:r2=1
The first 47518222 Occurrence results : r1=0:r2=0
Here's how , appear r1=0、r2=0 The case of indicates that there is an instruction reordering , We can check the code , The location of two code exchanges does not affect the result , So reordering can happen , For single threads , This reordering is no problem , But there will be problems in the case of multithreading concurrency , therefore r1=r2=0 It is because instruction reordering occurs in the case of multithreading .
From the above example, we can easily understand what is called Command rearrangement . To solve this problem, you can use primitives volatile Disable instruction reordering , The bottom layer is realized by four memory barrier instructions .
边栏推荐
- day17_ Under collection
- day10_ Exception handling & enumeration
- 5G控制面协议之N2接口
- Design and simulation code of 4-bit subtracter based on FPGA
- 最新PyCharm2018破解教程
- 为什么5G N2接口控制面使用SCTP协议?
- Hongke case | PAC: an integrated control solution integrating SoftPLC control logic, HMI and other service functions
- Joint modeling of price preference and interest preference in conversation recommendation - extensive reading of papers
- pairs和ipairs的区别
- 成长为架构师途中的一些思考
猜你喜欢

三、广域通信网

3、 Wide area communication network

MQTT服务器搭建以及使用MQTT.fx测试

VMware虚拟机在物理机win10系统下如何连接外网

centos 部署postgresql 13

day03_1_流程控制

FIR filter design (1) -- using the FDATool toolbox of MATLAB to design FIR filter parameters

Using STP spanning tree protocol to solve the problem of two-layer loop in network

Floating point multiplication and division of vivado IP core floating point

After the EtherCAT master station is disconnected, how to ensure that the target system is not affected by the fault?
随机推荐
day03_ 2_ task
STP spanning tree principle and example of election rules
What is DNS amplification attack
Callable 的使用
Network Security Learning (II)
Use of for statement in Verilog
day03_ 1_ Process control
最新PyCharm2018破解教程
DDoS details
Advanced socket programming (options and control information)
注解(Annotation)
Several misunderstandings about DDoS
Base64与File之间的相互转化
CNN-卷积神经网络
Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation
Hongke share | let you have a comprehensive understanding of "can bus error" (III) -- can node status and error counter
ss命令详解
5G控制面协议之N2接口
MySQL 事物四种隔离级别分析
如何在开发板上使用sftp命令访问sftp-server