- 論壇徽章:
- 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,是否能保證原子性未知。 |
|