documentation for AtomicExchange将方法签名赋予:
function AtomicExchange(var Target; Value: <Integer or NativeInt or Pointer>): Integer; overload;
function AtomicExchange(var Target; Value: <Integer or NativeInt or Pointer>): Int64; overload;
function AtomicExchange(var Target; Value: <Integer or NativeInt or Pointer>): Pointer; overload;
function AtomicExchange(var Target; Value: <Integer or NativeInt or Pointer>): NativeInt; overload;
但在实践中,似乎Int64
也是一个有效的参数,如:
function AtomicExchange(var Target; Value: <Integer or Int64 or NativeInt or Pointer>): Integer; overload;
function AtomicExchange(var Target; Value: <Integer or Int64 or NativeInt or Pointer>): Int64; overload;
function AtomicExchange(var Target; Value: <Integer or Int64 or NativeInt or Pointer>): Pointer; overload;
function AtomicExchange(var Target; Value: <Integer or Int64 or NativeInt or Pointer>): NativeInt; overload;
注意:除文档外,IDE工具提示还建议Int64
不是有效参数:
事实上,我们可以使用AtomicExchange
参数调用Int64
,这是否可以正常用于32位和64位目标?
答案 0 :(得分:5)
文档不正确,AtomicExchange
可用于64位操作数。许多内在函数都记录不正确,这是另一个。
查看包含原子内部函数的TInterlocked
类方法,它更加清晰。有这种方法:
class function TInterlocked.Exchange(var Target: Int64; Value: Int64): Int64;
begin
Result := AtomicExchange(Target, Value);
end;
这清楚地表明AtomicExchange
完全支持64位值,即使在32位进程中也是如此。