开发者

Does Interlocked.CompareExchange(double,double,double) work in 32 bit OS?

开发者 https://www.devze.com 2022-12-14 19:12 出处:网络
I\'m maintaining a high performance class that can be operated on by multiple threads.Many of 开发者_如何学Pythonthe fields are volatile ints, and as it turns out I need to upgrade one of those to a d

I'm maintaining a high performance class that can be operated on by multiple threads. Many of 开发者_如何学Pythonthe fields are volatile ints, and as it turns out I need to upgrade one of those to a double. I'm curious if there is a lock free way to do this, and was wondering if the Interlocked.CompareExchange(double, double, double) works as advertised on a 32-bit OS, or are torn reads a problem.


This page details the intrinsics of the "native" Interlocked functions. It mentions the following limitations

Because _InterlockedCompareExchange64 uses the cmpxchg8b instruction, it is not available on pre-Pentium processors, such as the 486.

So we can expect that it is available and also implemented as an interlocked instruction operation (rather than being simulated by using a full lock).


Yes, it works as described on 32-bit. That's what the Interlocked methods are there for.


Yes, it's works.

It uses InterlockedCompareExchange64 - if you want to have a look at a possible implementation in x86 asm, have a look here - http://qc.embarcadero.com/wc/qcmain.aspx?d=6212.

asm
//     ->          EAX     Destination 
//                 ESP+4   Exchange    
//                 ESP+12  Comperand   
//     <-          EDX:EAX Result      
          PUSH    EBX
          PUSH    EDI

          MOV     EDI, EAX

          MOV     EAX, DWORD PTR [Comperand]
          MOV     EDX, DWORD PTR [Comperand+4]

          MOV     EBX, DWORD PTR [Exchange]
          MOV     ECX, DWORD PTR [Exchange+4]

LOCK      CMPXCHG8B [EDI]

          POP     EDI
          POP     EBX
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消