"thread-safe way to increment and return an integer in delphi" Code Answer

3

you can use the interlocked* functions:

    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:

    function getnextuid : integer;
    {$j+} // writeble constants
    const
      cuid : integer = 0;
    begin
      result := atomicincrement(cuid);
    end;
By Sleek Geek on June 24 2022

Answers related to “thread-safe way to increment and return an integer in delphi”

Only authorized users can answer the Search term. Please sign in first, or register a free account.