首页 > 其他分享 >待整理

待整理

时间:2024-03-20 16:24:05浏览次数:23  
标签:function Target overload var static 整理 class

unit System 单元下有这些函数

image

在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;

image

标签:function,Target,overload,var,static,整理,class
From: https://www.cnblogs.com/del88/p/18085490

相关文章

  • Python就该这样学,纯小白速通Python!学习大纲整理,建议保存
    一、学习建议1、找到自己感兴趣的方向,并且结合市场需求进行选择Python的应用范围测试运维web人工智能大数据爬虫及数据分析办公自动化2、学习过程中一定要勤加练习,并且尝试去使用学习过的内容实现一些简答的功能遇到技术问题不要慌,解决问题的过程也是加速自己成长的途......
  • opengauss-jdbc问题整理
    opengauss-jdbc问题整理(更新中)问题1jdbc批量执行insert语句时返回结果不符合Springjpa预期问题描述:jdbc执行查询时,可以使用preparestatment.executeBatch()方法批量执行一组sql语句,该方法返回为int[]int型数组变量,含义是批量执行的每个sql语句更新的数据行数。......
  • 04747 JAVA语言设计必备考点整理(一)
    标识符的命名规则由英文字母大小写,数字,下划线,美元符组成的字符串,其中数字不能作为标识符的开头,区分大小写 package语句和import语句的区别package语句:包语句,每个文件最多有一个,必须放在文件开始的地方import语句:引入语句,可以没有,可以由多个。如......
  • 软考高项学习重点总结整理第1章信息化发展
     以下内容均为作者个人根据“往年软考高项考题”及考试重点整理的选择题重要考点。不喜欢看厚厚书本的友友的福音......
  • 面试题整理
    1、自我介绍面试官好,我叫。我有5年多的软件测试经验。在工作中,熟练掌握了各种测试方法和工具,包括黑盒测试、白盒测试、自动化测试、性能测试、安全测试等。有一定的编程能力,能够根据需要编写测试用例和脚本。有良好的沟通能力和团队协作能力,能够与开发人员、产品经理等各部门人员......
  • 整理记录list(未完成)
    1.seurat4环境的安装代码2.conda安装知识的总结()https://www.jianshu.com/p/4c7b9127cf83conda常见报错conda-channel问题http://mp.weixin.qq.com/s?__biz=Mzg5MjY1MTU4Ng==&mid=2247484430&idx=1&sn=7df7c1d546ff5fce3e5d6e73f7569c2c&chksm=c03b9523f74c1c35e96bf287e959......
  • CSPJ知识点整理
    指针:https://blog.csdn.net/qq_35429198/article/details/109331937排序:https://www.cnblogs.com/myeln/articles/17576193.html递归作业:https://www.luogu.com.cn/training/370327#problems哈夫曼编码:https://zhuanlan.zhihu.com/p/415467000图:https://www.luogu.com.cn/trai......
  • 整理h5进行微信支付的注意事项
    前言微信支付流程比支付宝的复杂的多限制也多,尤其是当你有多个公众号多个openid的时候,如何合理规划是非常重要的一环。我们首先需要在微信公众号申请开通微信支付功能,然后在用户进入我们页面时就触发授权(分为静默授权和手动授权),授权成功之后,拿到openid就可以进行创建订单--支......
  • SqlServer2008(R2)(一)SqlServer2008(R2)经典宝藏操作收集整理
    一、常见操作1、TRUNCATETABLE语句删除表数据TRUNCATETABLE语句比DELET删除表中的所有行更快。从逻辑上讲,TRUNCATETABLE它类似于DELETE没有WHERE子句的语句。TRUNCATETABLE语句从表中删除所有行,但表结构及其列,约束,索引等保持不变。要删除表及其数据,可以使用该DROP......
  • Gradle知识点整理(1)
    Gradle知识点整理(1)文章目录Gradle知识点整理(1)常见的项目构建工具Gradle安装Gradle和Maven项目结构修改Maven下载源GradleWrapperGroovy学习Gretty项目部署Gradle项目生命周期Gradle中的setting文件Gradle的Task常见的项目构建工具Ant:优点:使用灵活,速度快(快......