指令重新排序

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

问题描述

各位大家好!


假设我有以下功能:


void

call_once(int *完成,无效(*例程)(无效))

{

if(!(* done)){/ * L1 * /

routine(); / * L2 * /

* done = 1; / * L3 * /

}

}


符合ANSI C标准的编译器有权重新订购L2和L3,即

执行以下序列:

* done = 1;

routine();


提前致谢!

Loic。

解决方案



当且仅当它知道两个序列的行为

相同时才允许这样做。因此,如果它不知道常规

是否可以访问*完成,则无法重新排序语句。





当且仅当它知道两个序列的行为

相同时才允许这样做。因此,如果它不知道常规

是否可以访问*完成,则无法重新排序语句。



那可能只有在


call_once(int * restrict done,void(* routine)(void))时才能完成


如果限制关键词本来是肯定的,你可以做到。




当且仅当它知道两个序列的行为

相同时才允许这样做。因此,如果它不知道例行程序是否可以访问*完成,它就不能对语句重新排序。



只有在


call_once(int * restrict done,void(* routine)(void))时才可以这样做


如果限制关键字会被使用是的,你可以做到。



如果编译器选择内联特定的调用

来调用call_once,也可以这样做。


Hello everybody!

Assume that I have the following function:

void
call_once(int* done, void(*routine)(void))
{
if (!(*done)) { /* L1 */
routine(); /* L2 */
*done = 1; /* L3 */
}
}

Is an ANSI C compliant compiler authorized to re-order L2 and L3, i.e.
execute the following sequence instead:
*done = 1;
routine();

Thanks in advance!
Loic.

解决方案

It is allowed to do that if and only if it can know that the behaviour
for both sequences is the same. So if it doesn''t know whether routine
can access *done, it can''t reorder the statements.




It is allowed to do that if and only if it can know that the behaviour
for both sequences is the same. So if it doesn''t know whether routine
can access *done, it can''t reorder the statements.

That could be done only if

call_once(int* restrict done, void(*routine)(void))

If the "restrict" keywords would have been used yes, you could have done it.



It is allowed to do that if and only if it can know that the behaviour
for both sequences is the same. So if it doesn''t know whether routine
can access *done, it can''t reorder the statements.


That could be done only if

call_once(int* restrict done, void(*routine)(void))

If the "restrict" keywords would have been used yes, you could have done it.

It can also be done if the compiler chooses to inline a specific call
to call_once.


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

08-03 21:49
查看更多