- 论坛徽章:
- 1
|
回复 #6 mik 的帖子
刚测试了3种情况
1
- int a = 1;
- int main(void)
- {
- int b = a;
- return 0;
- }
复制代码
- .file "test1_atomic.c"
- .globl a
- .data
- .align 4
- .type a, @object
- .size a, 4
- a:
- .long 1
- .text
- .globl main
- .type main, @function
- main:
- pushl %ebp
- movl %esp, %ebp
- subl $16, %esp
- movl a, %eax // ***
- movl %eax, -4(%ebp) // ***
- movl $0, %eax
- leave
- ret
- .size main, .-main
- .ident "GCC: (GNU) 4.4.2 20091027 (Red Hat 4.4.2-7)"
- .section .note.GNU-stack,"",@progbits
复制代码
2
- int a = 1;
- int b = 0;
- int main(void)
- {
- b = a;
- return 0;
- }
复制代码
- .file "test2_atomic.c"
- .globl a
- .data
- .align 4
- .type a, @object
- .size a, 4
- a:
- .long 1
- .globl b
- .bss
- .align 4
- .type b, @object
- .size b, 4
- b:
- .zero 4
- .text
- .globl main
- .type main, @function
- main:
- pushl %ebp
- movl %esp, %ebp
- movl a, %eax // ***
- movl %eax, b // ***
- movl $0, %eax
- popl %ebp
- ret
- .size main, .-main
- .ident "GCC: (GNU) 4.4.2 20091027 (Red Hat 4.4.2-7)"
- .section .note.GNU-stack,"",@progbits
复制代码
3
- int main(void)
- {
- int a = 1;
- int b = a;
- return 0;
- }
复制代码
- .file "test3_atomic.c"
- .text
- .globl main
- .type main, @function
- main:
- pushl %ebp
- movl %esp, %ebp
- subl $16, %esp
- movl $1, -8(%ebp)
- movl -8(%ebp), %eax // ***
- movl %eax, -4(%ebp) // ***
- movl $0, %eax
- leave
- ret
- .size main, .-main
- .ident "GCC: (GNU) 4.4.2 20091027 (Red Hat 4.4.2-7)"
- .section .note.GNU-stack,"",@progbits
复制代码
这样看b=a对应2条汇编语句,而且是通过mov操作完成的
感觉两条语句之间是有时间间隔的,这样CPU也能保证原子性?
而且Intel手册上说的也仅仅是满足一些条件的read or write操作可以保证原子性,这里涉及两个操作read and write,是否能保证原子性未知啊! |
|