Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

指令重排一定对单线程没有影响吗

test(){
    save(A);  // 操作1 保存资源A
    save(B);  // 操作2 保存资源B
}

1 单线程场景、操作1和操作2之前没有依赖关系,是否会出现指令重排?
2 如果出现指令重排,save(B)先执行,之后执行save(A),如果此时save(A)失败且抛出异常,那结果就是只成功save(B)吗?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

也许会重排,也许不会,因为即使发生重排,要重排的也是指令,不是你这两行代码的顺序。

对操作系统而言指令都是汇编级别的。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...