unit System 单元下有这些函数
在System单元里 搜不到这些函数的具体定义,只可以这样智能提示看到;
另外在 System.SyncObjs 有一个TInterlocked的密封类,其十多个类函数(class function)其实都是调用的System单元的原子操作函数,只是封装得更容易理解。
{ TInterlocked implements various common atomic opererations for the purpose of ensuring "thread" or "multi-core"
safety when modifying variables that could be accessed from multiple threads simultaneously. The TInterlocked class
is not intended to be instantiated nor derived from. All the methods are "class static" and are merely defined in
a class as a way to group their like-functionality.
}
{
中文翻译:TInterlocked实现了各种常见的原子操作,以确保“线程”或“多核”
在修改可以从多个线程同时访问的变量时,可以确保其安全性。TInterlocked 类
不是用来实例化的,也不是从它派生出来的。所有的方法都是“类静态的”,只是在
将类作为将具有相似功能的事物进行分组的一种方式。
}
TBitOffset = 0..31;
TBitOffset64 = 0..63;
TInterlocked = class sealed
public
class function Increment(var Target: Integer): Integer; overload; static; inline;
class function Increment(var Target: Cardinal): Cardinal; overload; static; inline;
class function Increment(var Target: Int64): Int64; overload; static; inline;
class function Increment(var Target: UInt64): UInt64; overload; static; inline;
class function Decrement(var Target: Integer): Integer; overload; static; inline;
class function Decrement(var Target: Cardinal): Cardinal; overload; static; inline;
class function Decrement(var Target: Int64): Int64; overload; static; inline;
class function Decrement(var Target: UInt64): UInt64; overload; static; inline;
class function Add(var Target: Integer; Increment: Integer): Integer; overload; static; inline;
class function Add(var Target: Cardinal; Increment: Cardinal): Cardinal; overload; static; inline;
class function Add(var Target: Int64; Increment: Int64): Int64; overload; static; inline;
class function Add(var Target: UInt64; Increment: UInt64): UInt64; overload; static; inline;
class function BitTestAndSet(var Target: Integer; BitOffset: TBitOffset): Boolean; overload; static;
class function BitTestAndSet(var Target: Cardinal; BitOffset: TBitOffset): Boolean; overload; static;
class function BitTestAndSet(var Target: Int64; BitOffset: TBitOffset64): Boolean; overload; static;
class function BitTestAndSet(var Target: UInt64; BitOffset: TBitOffset64): Boolean; overload; static;
class function BitTestAndClear(var Target: Integer; BitOffset: TBitOffset): Boolean; overload; static;
class function BitTestAndClear(var Target: Cardinal; BitOffset: TBitOffset): Boolean; overload; static;
class function BitTestAndClear(var Target: Int64; BitOffset: TBitOffset64): Boolean; overload; static;
class function BitTestAndClear(var Target: UInt64; BitOffset: TBitOffset64): Boolean; overload; static;
class function Exchange(var Target: Pointer; Value: Pointer): Pointer; overload; static; inline;
class function Exchange(var Target: Integer; Value: Integer): Integer; overload; static; inline;
class function Exchange(var Target: Cardinal; Value: Cardinal): Cardinal; overload; static; inline;
class function Exchange(var Target: Int64; Value: Int64): Int64; overload; static; inline;
class function Exchange(var Target: UInt64; Value: UInt64): UInt64; overload; static; inline;
class function Exchange(var Target: TObject; Value: TObject): TObject; overload; static; inline;
class function Exchange(var Target: Single; Value: Single): Single; overload; static; inline;
class function Exchange(var Target: Double; Value: Double): Double; overload; static; inline;
class function Exchange(var Target: Boolean; Value: Boolean): Boolean; overload; static; inline;
class function Exchange<T: class>(var Target: T; Value: T): T; overload; static; inline;
class function CompareExchange(var Target: Pointer; Value: Pointer; Comparand: Pointer): Pointer; overload; static; inline;
class function CompareExchange(var Target: Pointer; Value: Pointer; Comparand: Pointer; out Succeeded: Boolean): Pointer; overload; static; inline;
class function CompareExchange(var Target: Integer; Value: Integer; Comparand: Integer): Integer; overload; static; inline;
class function CompareExchange(var Target: Integer; Value: Integer; Comparand: Integer; out Succeeded: Boolean): Integer; overload; static;
class function CompareExchange(var Target: Cardinal; Value: Cardinal; Comparand: Cardinal): Cardinal; overload; static; inline;
class function CompareExchange(var Target: Cardinal; Value: Cardinal; Comparand: Cardinal; out Succeeded: Boolean): Cardinal; overload; static; inline;
class function CompareExchange(var Target: Int64; Value: Int64; Comparand: Int64): Int64; overload; static; inline;
class function CompareExchange(var Target: Int64; Value: Int64; Comparand: Int64; out Succeeded: Boolean): Int64; overload; static; inline;
class function CompareExchange(var Target: UInt64; Value: UInt64; Comparand: UInt64): UInt64; overload; static; inline;
class function CompareExchange(var Target: UInt64; Value: UInt64; Comparand: UInt64; out Succeeded: Boolean): UInt64; overload; static; inline;
class function CompareExchange(var Target: TObject; Value: TObject; Comparand: TObject): TObject; overload; static; inline;
class function CompareExchange(var Target: TObject; Value: TObject; Comparand: TObject; out Succeeded: Boolean): TObject; overload; static; inline;
class function CompareExchange(var Target: Double; Value: Double; Comparand: Double): Double; overload; static; inline;
class function CompareExchange(var Target: Double; Value: Double; Comparand: Double; out Succeeded: Boolean): Double; overload; static; inline;
class function CompareExchange(var Target: Single; Value: Single; Comparand: Single): Single; overload; static; inline;
class function CompareExchange(var Target: Single; Value: Single; Comparand: Single; out Succeeded: Boolean): Single; overload; static; inline;
class function CompareExchange(var Target: Boolean; Value: Boolean; Comparand: Boolean): Boolean; overload; static; inline;
class function CompareExchange(var Target: Boolean; Value: Boolean; Comparand: Boolean; out Succeeded: Boolean): Boolean; overload; static; inline;
class function CompareExchange<T: class>(var Target: T; Value: T; Comparand: T): T; overload; static; inline;
class function CompareExchange<T: class>(var Target: T; Value: T; Comparand: T; out Succeeded: Boolean): T; overload; static; inline;
class function Read(var Target: Int64): Int64; overload; static; inline;
class function Read(var Target: UInt64): UInt64; overload; static; inline;
end;
使用方法:如对一个数值加一,则直接b:= TInterlocked.Increment(a);或TInterlocked.Increment(a);,不用创建TInterlocked类(类函数相当于就是单独的函数,可以直接调用,不用实例化对象)
大致就是4种,递增、递减、交换、比较交换;我们来一一研究测试一下;
递增、递减
procedure TForm2.btn1Click(Sender: TObject);
var
i: Integer;
i4: Int64;
begin
i := 0;
AtomicIncrement(i, 2);
OutputDebugString(PChar(i.ToString));//2
AtomicDecrement(i, 3);
OutputDebugString(PChar(i.ToString));//-1
i4 := 0;
AtomicIncrement(i4, 2);
OutputDebugString(PChar(i4.ToString)); //2
AtomicDecrement(i4, 3);
OutputDebugString(PChar(i4.ToString));//-1
end;
交换
有3个重载,Integer、Int64、Pointer
procedure TForm2.btn1Click(Sender: TObject);
var
i, r: Integer;
b: Boolean;
begin
i := 0;
r := AtomicCmpExchange(i, 2, 2, b);
OutputDebugString(PChar(i.ToString));// 0 , 没有替换成功,i还是旧值
OutputDebugString(PChar(r.ToString));// 0 返回的依然是旧值
OutputDebugString(PChar(b.ToString(TUseBoolStrs.True)));// False
OutputDebugString('----------------------------');
i := 0;
r := AtomicCmpExchange(i, 2, 0, b);
OutputDebugString(PChar(i.ToString));// 2
OutputDebugString(PChar(r.ToString));// 0 , 说明返回的还是原来的旧值
OutputDebugString(PChar(b.ToString(TUseBoolStrs.True)));// True
end;
标签:function,Target,overload,var,static,整理,class
From: https://www.cnblogs.com/del88/p/18085490