当前位置:网站首页>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 .
边栏推荐
- Hongke solution | a unique solution to realize seamless integration at low cost in Digital Substations
- 8、 Network security
- What is the basic principle of Library collision and library collision attack
- MySQL 事物四种隔离级别分析
- 什么是DNS放大攻击
- 失效的访问控制
- Solve the error that the simulation output is STX under the frequency division module Modelsim
- 5G服务化接口和参考点
- Hongke shares | why EtherCAT is the best solution to improve the performance of the control system?
- Raw advanced socket experiment
猜你喜欢

Hongke share | FPGA implementation of pass through and store and forward switching delay

day06_ Classes and objects

基于噪声伪标签和对抗性学习的医学图像分割注释有效学习

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

软件定义边界SDP

After the EtherCAT master station is disconnected, how to ensure that the target system is not affected by the fault?

Condition 条件对象源码浅读

Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (3) -- modk routine test

注解(Annotation)

Merkletree builds QT implementation UI
随机推荐
Introduction to OSPF theory
Instant 新日期类的使用 API
10种常见的软件架构模式
Annotation
Conversion of fixed-point number to floating-point number of vivado IP core
TCP socket communication experiment
损失函数——交叉熵损失函数
Solution for website being suspended
What is the basic principle of Library collision and library collision attack
Merkle tree existential function modified for the first time
day02_基本语法
案例补充、ATM
day10_ Exception handling & enumeration
Understanding of access, hybrid and trunk modes
Design and simulation code of 4-bit subtracter based on FPGA
CNAME记录和A记录的区别
Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (3) -- modk routine test
比较单片机3种时钟电路方案
基于噪声伪标签和对抗性学习的医学图像分割注释有效学习
Inventory | major network security events of global key information infrastructure