System.AtomicCmpExchange

De RAD Studio API Documentation
Aller à : navigation, rechercher

Delphi

function AtomicCmpExchange(var Target; NewValue: <Integer or NativeInt or Pointer>; Comparand: <Integer or NativeInt or Pointer>; [out Succeeded: Boolean]): Integer; overload;
function AtomicCmpExchange(var Target; NewValue: <Integer or NativeInt or Pointer>; Comparand: <Integer or NativeInt or Pointer>; [out Succeeded: Boolean]): Int64; overload;
function AtomicCmpExchange(var Target; NewValue: <Integer or NativeInt or Pointer>; Comparand: <Integer or NativeInt or Pointer>; [out Succeeded: Boolean]): Pointer; overload;
function AtomicCmpExchange(var Target; NewValue: <Integer or NativeInt or Pointer>; Comparand: <Integer or NativeInt or Pointer>; [out Succeeded: Boolean]): NativeInt; overload;
function AtomicCmpExchange(var Target; NewValue: <Integer or NativeInt or Pointer>; Comparand: <Integer or NativeInt or Pointer>; [out Succeeded: Boolean]): Integer; overload;
function AtomicCmpExchange(var Target; NewValue: <Integer or NativeInt or Pointer>; Comparand: <Integer or NativeInt or Pointer>; [out Succeeded: Boolean]): Int64; overload;
function AtomicCmpExchange(var Target; NewValue: <Integer or NativeInt or Pointer>; Comparand: <Integer or NativeInt or Pointer>; [out Succeeded: Boolean]): Pointer; overload;
function AtomicCmpExchange(var Target; NewValue: <Integer or NativeInt or Pointer>; Comparand: <Integer or NativeInt or Pointer>; [out Succeeded: Boolean]): NativeInt; overload;

Propriétés

Type Visibilité  Source Unité  Parent
function public System.pas System System


Description

La fonction intrinsèque atomique Compare and Exchange compare le contenu de Target par rapport à une valeur donnée (Comparand) et, uniquement dans le cas où les valeurs sont semblables, remplace le contenu de Target par la nouvelle valeur.

Cette fonction renvoie toujours la valeur originale de la cible. Dans le cas où un paramètre Succeeded est fourni, Succeeded vaut True s'il y a un échange de valeurs (même si la cible et la nouvelle valeur sont identiques), sinon il vaut False.

L'unité System fournit quatre fonctions intrinsèques atomiques qui procurent un moyen d'échanger, comparer et échanger, incrémenter et décrémenter des valeurs en mémoire.

La fonction AtomicCmpExchange est utilisée pour comparer et échanger des valeurs en mémoire.

Les opérations atomiques sont utilisées pour :

  • Implémenter des primitives de verrou multi-thread.
  • Fournir les primitives nécessaires pour implémenter des structures "libres de tout verrou".

Exemple de code

program SystemAtomicCmpExchange;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, Winapi.Windows;

var
  Target,
  Comparand,
  Return : Integer;
  Succeeded : Boolean;

const
  ReplaceValue = Integer(5);

begin
  try
    WriteLn('System.AtomicCmpExchange');
    WriteLn;

    Target := 1;
    Comparand := 0;
    WriteLn(Format('Initial values: Target: %d, Comparand: %d, ReplaceValue : %d',
      [Target, Comparand, ReplaceValue]));
    Return := System.AtomicCmpExchange(Target, ReplaceValue, Comparand, Succeeded);
    WriteLn(Format('After calling:  Target: %d, Comparand: %d, Return: %d, Succeeded: %s',
      [Target, Comparand, Return, BoolToStr(Succeeded, true)]));

    WriteLn;

    Target := 1;
    Comparand := 1;
    WriteLn(Format('Initial values: Target: %d, Comparand: %d, ReplaceValue : %d',
      [Target, Comparand, ReplaceValue]));
    Return := System.AtomicCmpExchange(Target, ReplaceValue, Comparand, Succeeded);
    WriteLn(Format('After calling:  Target: %d, Comparand: %d, Return: %d, Succeeded: %s',
      [Target, Comparand, Return, BoolToStr(Succeeded, true)]));

    WriteLn;
    WriteLn('InterlockedCompareExchange');
    WriteLn;

    Target := 1;
    Comparand := 0;
    WriteLn(Format('Initial values: Target: %d, Comparand: %d, ReplaceValue : %d',
      [Target, Comparand, ReplaceValue]));
    Return := InterlockedCompareExchange(Target, ReplaceValue, Comparand);
    WriteLn(Format('After calling:  Target: %d, Comparand: %d, Return: %d',
      [Target, Comparand, Return]));

    WriteLn;

    Target := 1;
    Comparand := 1;
    WriteLn(Format('Initial values: Target: %d, Comparand: %d, ReplaceValue : %d',
      [Target, Comparand, ReplaceValue]));
    Return := InterlockedCompareExchange(Target, ReplaceValue, Comparand);
    WriteLn(Format('After calling:  Target: %d, Comparand: %d, Return: %d',
      [Target, Comparand, Return]));
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Voir aussi