InterlockedExchangeAdd()“对Addend指向的值执行Value的原子加法,结果存储在Addend指定的地址中.
System.SyncObjs单元有一个TInterlocked类,它已经超载了Add()的方法来做同样的事情:
Increments an integer value with another.
There are two overloaded Add methods. Both Add methods increment a Target by Increment.
class function Add(var Target: Integer; Increment: Integer): Integer; overload; static; inline;
class function Add(var Target: Int64; Increment: Int64): Int64; overload; static; inline;
区别在于InterlockedExchangeAdd()“返回由Addend指向的变量的初始值,而TInterlocked.Add()”则返回递增参数的值“.所以,如果你使用返回值,你将不得不说明这个差异,例如:
function InterlockedExchangeAdd(var Addend: Integer; Value: Integer): Integer;
begin
Result := TInterlocked.Add(Addend, Value) - Value;
end;