简体   繁体   English

在Delphi中以线程安全的方式增加和返回一个整数

[英]Thread-safe way to increment and return an integer in Delphi

In a single-threaded application I use code like this: 在单线程应用程序中,我使用如下代码:

Interface
    function GetNextUID : integer;
Implementation
    function GetNextUID : integer;
    const
      cUID : integer = 0;
    begin
      inc( cUID );
      result := cUID;
    end;

This could of course be implemented as a singleton object, etc. - I'm just giving the simplest possible example. 这当然可以作为单个对象实现,等等 - 我只是给出了最简单的例子。

Q: How can I modify this function (or design a class) to achieve the same result safely from concurrent threads? 问:如何修改此函数(或设计类)以从并发线程安全地实现相同的结果?

You can use the Interlocked* functions: 您可以使用Interlocked*功能:

    function GetNextUID : integer;
    {$J+} // Writeble constants
    const
      cUID : integer = 0;
    begin
      Result := InterlockedIncrement(cUID);
    end;

More modern Delphi versions have renamed these methods into Atomic* (like AtomicDecrement , AtomicIncrement , etc), so the example code becomes this: 更现代的Delphi版本已将这些方法重命名为Atomic* (如AtomicDecrementAtomicIncrement等),因此示例代码变为:

    function GetNextUID : integer;
    {$J+} // Writeble constants
    const
      cUID : integer = 0;
    begin
      Result := AtomicIncrement(cUID);
    end;

最简单的方法可能就是调用InterlockedIncrement来完成这项工作。

With modern Delphi compilers it is better to use Increment function of class TInterlocked from unit System.SyncObjs. 使用现代Delphi编译器,最好从System.SyncObjs单元使用TInterlocked类的Increment函数。 Something like this: 像这样的东西:

  type
    TMyClass = class
    class var
      FIdCounter: int64;
    var
      FId: int64;
    constructor Create;
    end;

constructor TMyClass.Create;
begin
  FId := TInterlocked.Increment(FIdCounter);
end;

This helps to keep the code platform-independent. 这有助于保持代码平台无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM