乱序执行/重新排序指令 [英] out of order execution / reoredering of instructions

查看:77
本文介绍了乱序执行/重新排序指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在一些文章中读到有两种类型的

"重新排序>

指示。一个在编译器级别,另一个在

处理器/ CPU

级别。这两者有什么区别?


C程序员如何能够通过

编译器来防止重新排序指令

和由CPU?

在什么情况下,程序员应该处理这些重新排序?


我可以通过以下方式抑制重新排序(通过编译器)使用内存屏障?


例如,我想按顺序更新两个内存位置,可以

我这样做如下:


func()

{

int * ptr1; int * ptr2;


/ *代码,其中ptr1和ptr2被初始化* /


* ptr1 = some_val; / * * ptr1应该在* ptr2之前更新

* /


mb(); / *记忆障碍例程* /


/ *一些代码* /


* ptr2 = some_other_val;

}


我想知道是否调用mb(),确保订购两个更新。

Hi,

I read in some of the articles that there are two types of
"reordering"
of instructions. One at the compiler level and other at the
processor/CPU
level. What is the difference between these two ?

How can a C programmer, prevent the reordering of instructions by
compiler
and by the CPU ?

In what cases, the programmer should take care of these reorderings ?

Can I suppress reordering (by compiler ) by using memory barriers ?

For instance, I want to update two memory locations in order, can
I do it as follows:

func()
{
int *ptr1; int *ptr2;

/*code where ptr1 and ptr2 are initialized */

*ptr1 = some_val; /* *ptr1 should be updated before *ptr2
*/

mb(); /* memory barrier routine */

/* some code */

*ptr2 = some_other_val;
}

I want to know if calling mb(), ensures ordering of the two updates.

推荐答案

ju ********** @ yahoo.co.in 说:

怎样一个C程序员,可以通过

编译器来防止重新排序指令

和由CPU?
How can a C programmer, prevent the reordering of instructions by
compiler
and by the CPU ?



您可以通过仔细使用序列

积分来强制执行您想要的程序逻辑。从语言的角度来看,没有任何机制可以指示编译器如何翻译你的代码。

You can enforce the program logic you want by careful use of sequence
points. From a language perspective, there is no mechanism for dictating to
the compiler how it should translate your code.


在什么情况下,程序员应该处理这些重新排序?
In what cases, the programmer should take care of these reorderings ?



只要程序正确计算结果,我们为什么要关心

命令发生的事情?


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

As long as the program computes the result properly, why should we care what
order things happen in?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


ju ********** @ yahoo.co.in écrit:


看看junky_fellow,为什么这对你来说是一个问题?

你为什么要按自己的方式订购说明?


你怀疑编译器有错误吗?


请解释。
ju**********@yahoo.co.in a écrit :

Look junky_fellow, why is this a problem for you?
Why do you want to order the instructions your way?

You suspect a bug in the compiler?

Please explain.




Richard Heathfield写道:

Richard Heathfield wrote:
ju ********** @ yahoo.co.in 说:

怎样一个C程序员,可以通过

编译器来防止重新排序指令

和by CPU?
How can a C programmer, prevent the reordering of instructions by
compiler
and by the CPU ?



您可以通过仔细使用序列

积分来强制执行您想要的程序逻辑。从语言的角度来看,没有任何机制可以指示编译器如何翻译你的代码。


You can enforce the program logic you want by careful use of sequence
points. From a language perspective, there is no mechanism for dictating to
the compiler how it should translate your code.


在什么情况下,程序员应该处理这些重新排序?
In what cases, the programmer should take care of these reorderings ?



只要程序正确计算结果,我们为什么要关心

命令发生的事情?


As long as the program computes the result properly, why should we care what
order things happen in?



在设备驱动程序代码中,我们需要将一些特定值写入

设备以某种特定顺序注册。如果更改了序列

,则设备可能无法正常工作。在这种情况下,

我们如何防止重新排序?


在多处理器环境中,多个线程可以并行执行
不同的处理器,需要全局变量(多线程可以访问)可以由特定线程按某种顺序修改
。怎么样,

这样可以实现吗?

In a device driver code, we need to write some specific value to
the device registers in some specific sequence. If that sequence
is changed, the device may not work as desired. In such cases,
how can we prevent reordering ?

In a multiprocessor environment, where multiple threads may
execute parallelly on different processors, it is required that
the global variables (that multiple threads may access) may
be modified in some sequence by a particular thread. How,
such can this be achieved ?


这篇关于乱序执行/重新排序指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆