- 论坛徽章:
- 0
|
是A.
下面是汇编代码.
其中A是把y的内容放到一个寄存器里,之后又把寄存器的内容放到x里.
在这之间,y的值可能会被改变,比如有两个线程,有如下代码:
x=y;
____________
x=z;
____________
我们不能指望,x=y执行后,x和y的值是相等的,所以,并非安全.
而其它的选项,都是安全的.
问题是: x++和++x是否安全,
而不是y=x++或y=++x是否安全,
y=x++和y=++x都不是安全的,但x++和++x本身都是安全的.
void test1()
{
int x=23;
int y;
y=x;
}
void test2()
{
int x=23;
int y;
x++;
}
void test3()
{
int x=23;
int y;
++x;
}
void test4()
{
int x=23;
int y;
x=1;
}
_test1:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl $23, -4(%ebp)
movl -4(%ebp), %eax
movl %eax, -8(%ebp)
leave
ret
.globl _test2
.def _test2; .scl 2; .type 32; .endef
_test2:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl $23, -4(%ebp)
leal -4(%ebp), %eax
incl (%eax)
leave
ret
.globl _test3
.def _test3; .scl 2; .type 32; .endef
_test3:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl $23, -4(%ebp)
leal -4(%ebp), %eax
incl (%eax)
leave
ret
.globl _test4
.def _test4; .scl 2; .type 32; .endef
_test4:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl $23, -4(%ebp)
movl $1, -4(%ebp)
leave
ret
.def ___main; .scl 2; .type 32; .endef |
|