当前位置:网站首页>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 .
边栏推荐
- Conversion of fixed-point number to floating-point number of vivado IP core
- Solution for website being suspended
- 有用网站
- 成长为架构师途中的一些思考
- Design of IIR filter based on FPGA
- Merkle tree existential function modified for the first time
- etcd原理
- Understand the great changes of network security in five years
- Neuralcf neural collaborative filtering network
- 20个hacker神器
猜你喜欢

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

day10_ Exception handling & enumeration

5G服务化接口和参考点

Hongke share | let you have a comprehensive understanding of "can bus errors" (IV) -- producing and recording can errors in practice

Conversion of fixed-point number to floating-point number of vivado IP core

Hongke case | PAC: an integrated control solution integrating SoftPLC control logic, HMI and other service functions

Floating point square root of vivado IP core floating point

Floating point addition and subtraction method of vivado IP core floating point

Ansible(自动化软件)

day14_ Unit test & Date common class & String common class
随机推荐
Hongke share | FPGA implementation of pass through and store and forward switching delay
解决分频模块modelsim下仿真输出为stx的错误
将源码包转换为rpm包
多线程并发下的指令重排问题
6、 Network interconnection and Internet
MySQL 事物四种隔离级别分析
day03_2_作业
Ram block memory generator of vivado IP core
会话推荐中的价格偏好和兴趣偏好共同建模-论文泛读
3、 Wide area communication network
网络安全学习(一)
DDoS details
Embedding理解+代码
PhantomReference 虚引用代码演示
SQL developer graphical window to create database (tablespace and user)
Use of PDO
Complex floating point division of vivado IP core floating point
'function vtable for error: undefined reference to ... ' 问题的原因及解决方法
centos 部署postgresql 13
有用网站