Understanding Swift’s value type thread safety - 代码分析(一)
- 结构体并不代表线程安全,swift在此上未做保证
func testScenarioA() throws { var store: Int = 0 DispatchQueue.concurrentPerform(iterations: 1_000_000) { i in store = i _ = store } }
执行后下断点,对比寄存器和反汇编代码
(lldb) dis UnderstandStruct`closure #1 in testScenarioA(): 0x100002b60 <+0>: pushq %rbp 0x100002b61 <+1>: movq %rsp, %rbp 0x100002b64 <+4>: movq $0x0, -0x8(%rbp) 0x100002b6c <+12>: movq $0x0, -0x10(%rbp) 0x100002b74 <+20>: movq %rdi, -0x8(%rbp) 0x100002b78 <+24>: movq %rsi, -0x10(%rbp) -> 0x100002b7c <+28>: movq %rdi, (%rsi) 0x100002b7f <+31>: popq %rbp 0x100002b80 <+32>: retq 0x100002b81 <+33>: nopw %cs:(%rax,%rax) 0x100002b8b <+43>: nopl (%rax,%rax) (lldb) register read General Purpose Registers: rax = 0x00000001006236a0 rbx = 0x0000000000000000 rcx = 0x00007ffeefbff028 rdx = 0x00007fff6ac713a0 libswiftDispatch.dylib`___swift_dispatch_apply_current_block_invoke rdi = 0x0000000000000000 rsi = 0x00007ffeefbff1f0 rbp = 0x00007ffeefbfefb0 rsp = 0x00007ffeefbfefb0 r8 = 0x00007ffeefbff158 r9 = 0x0000000101400080 r10 = 0x00000000000020ff r11 = 0x0000000000000246 r12 = 0x00007ffeefbff158 r13 = 0x00007ffeefbff1d0 r14 = 0x0000000000000000 r15 = 0x00007fff6ac713a0 libswiftDispatch.dylib`___swift_dispatch_apply_current_block_invoke rip = 0x0000000100002b7c UnderstandStruct`closure #1 (Swift.Int) -> () in UnderstandStruct.testScenarioA() throws -> () + 28 at main.swift:14:15 rflags = 0x0000000000000246 cs = 0x000000000000002b fs = 0x0000000000000000 gs = 0x0000000000000000c
movq %rdi, (%rsi)
将寄存器rdi中的值赋给 rsi寄存器指向的地址所在的内存
rsi的内存指向的是 0x00007ffeefbff1f0 , 这个地址是指向 store变量
这个地址在多个线程中这个地址不变
- 综上,Int是值类型,但是从上面的代码看起来依然存在线程安全问题,多线程直接操作的是store的内存,虽然赋值是一条指令,但是可能产生ABA的问题。
- 从数据时序上看,不是安全的
- https://forums.swift.org/t/understanding-swifts-value-type-thread-safety/41406