Is the != check thread safe?

后端 未结 8 1846
醉酒成梦
醉酒成梦 2021-01-29 18:56

I know that compound operations such as i++ are not thread safe as they involve multiple operations.

But is checking the reference with itself a t

8条回答
  •  梦毁少年i
    2021-01-29 19:27

    It has all been well explained by Stephen C. For fun, you could try to run the same code with the following JVM parameters:

    -XX:InlineSmallCode=0
    

    This should prevent the optimisation done by the JIT (it does on hotspot 7 server) and you will see true forever (I stopped at 2,000,000 but I suppose it continues after that).

    For information, below is the JIT'ed code. To be honest, I don't read assembly fluently enough to know if the test is actually done or where the two loads come from. (line 26 is the test flag = a != a and line 31 is the closing brace of the while(true)).

      # {method} 'run' '()V' in 'javaapplication27/TestThreadSafety$1'
      0x00000000027dcc80: int3   
      0x00000000027dcc81: data32 data32 nop WORD PTR [rax+rax*1+0x0]
      0x00000000027dcc8c: data32 data32 xchg ax,ax
      0x00000000027dcc90: mov    DWORD PTR [rsp-0x6000],eax
      0x00000000027dcc97: push   rbp
      0x00000000027dcc98: sub    rsp,0x40
      0x00000000027dcc9c: mov    rbx,QWORD PTR [rdx+0x8]
      0x00000000027dcca0: mov    rbp,QWORD PTR [rdx+0x18]
      0x00000000027dcca4: mov    rcx,rdx
      0x00000000027dcca7: movabs r10,0x6e1a7680
      0x00000000027dccb1: call   r10
      0x00000000027dccb4: test   rbp,rbp
      0x00000000027dccb7: je     0x00000000027dccdd
      0x00000000027dccb9: mov    r10d,DWORD PTR [rbp+0x8]
      0x00000000027dccbd: cmp    r10d,0xefc158f4    ;   {oop('javaapplication27/TestThreadSafety$1')}
      0x00000000027dccc4: jne    0x00000000027dccf1
      0x00000000027dccc6: test   rbp,rbp
      0x00000000027dccc9: je     0x00000000027dcce1
      0x00000000027dcccb: cmp    r12d,DWORD PTR [rbp+0xc]
      0x00000000027dcccf: je     0x00000000027dcce1  ;*goto
                                                    ; - javaapplication27.TestThreadSafety$1::run@62 (line 31)
      0x00000000027dccd1: add    rbx,0x1            ; OopMap{rbp=Oop off=85}
                                                    ;*goto
                                                    ; - javaapplication27.TestThreadSafety$1::run@62 (line 31)
      0x00000000027dccd5: test   DWORD PTR [rip+0xfffffffffdb53325],eax        # 0x0000000000330000
                                                    ;*goto
                                                    ; - javaapplication27.TestThreadSafety$1::run@62 (line 31)
                                                    ;   {poll}
      0x00000000027dccdb: jmp    0x00000000027dccd1
      0x00000000027dccdd: xor    ebp,ebp
      0x00000000027dccdf: jmp    0x00000000027dccc6
      0x00000000027dcce1: mov    edx,0xffffff86
      0x00000000027dcce6: mov    QWORD PTR [rsp+0x20],rbx
      0x00000000027dcceb: call   0x00000000027a90a0  ; OopMap{rbp=Oop off=112}
                                                    ;*aload_0
                                                    ; - javaapplication27.TestThreadSafety$1::run@2 (line 26)
                                                    ;   {runtime_call}
      0x00000000027dccf0: int3   
      0x00000000027dccf1: mov    edx,0xffffffad
      0x00000000027dccf6: mov    QWORD PTR [rsp+0x20],rbx
      0x00000000027dccfb: call   0x00000000027a90a0  ; OopMap{rbp=Oop off=128}
                                                    ;*aload_0
                                                    ; - javaapplication27.TestThreadSafety$1::run@2 (line 26)
                                                    ;   {runtime_call}
      0x00000000027dcd00: int3                      ;*aload_0
                                                    ; - javaapplication27.TestThreadSafety$1::run@2 (line 26)
      0x00000000027dcd01: int3   
    

提交回复
热议问题