当前位置:网站首页>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 .
边栏推荐
- Navicat for Oracle Cannot create oci environment
- 一文看懂网络安全五年之巨变
- 9、 Networking technology
- Ansible(自动化软件)
- Best example of amortized cost
- Base64与File之间的相互转化
- Hog+svm for pedestrian detection
- Enterprise manager cannot connect to the database instance in Oracle10g solution
- Right value reference and mobile construction
- Hongke solution | a unique solution to realize seamless integration at low cost in Digital Substations
猜你喜欢
CNN-卷积神经网络
After the EtherCAT master station is disconnected, how to ensure that the target system is not affected by the fault?
What are the advantages of software testing? See how much you know
centos 部署postgresql 13
4、 LAN and man
基于噪声伪标签和对抗性学习的医学图像分割注释有效学习
会话推荐中的价格偏好和兴趣偏好共同建模-论文泛读
Hongke share | let you have a comprehensive understanding of "can bus errors" (IV) -- producing and recording can errors in practice
Hongke automation SoftPLC | modk operation environment and construction steps (1) -- Introduction to operation environment
Huawei switch ce12808 import and export configuration file
随机推荐
DDoS details
Network Security Learning (I)
Solution for website being suspended
基于噪声伪标签和对抗性学习的医学图像分割注释有效学习
LDAP简述及统一认证说明
【面试题】2022年最新软件测试面试题(400道)【附带答案】持续更新...
Using STP spanning tree protocol to solve the problem of two-layer loop in network
网络安全学习(一)
20个hacker神器
Computer right mouse click always turn around what's going on
FPGA - odd even frequency division and decimal frequency division code routine
Huawei switch ce12808 import and export configuration file
PhantomReference 虚引用代码演示
Floating point addition and subtraction method of vivado IP core floating point
day12_多线程
Ansible(自动化软件)
What is the basic principle of Library collision and library collision attack
IPv6 representation and configuration cases
day03_ 2_ task
Handwritten digit recognition using neural network