Q:

Where can I find TMonitor (Delphi 7) or how can I replace it with an alternative function?

I have a code (Singleton- Pattern) which works with Delphi RAD 10.1
type

  TSharedData = class
  private
    FPOL: integer;
    class var FUniqueInstance: TSharedData;
    procedure SetFPol(const Value: integer);
    constructor Create;
  public
    class function GetInstance: TSharedData;
    property POL: integer read FPOL write SetFPol;
  end;

var
  Key: TObject;

implementation

{ TSharedData }

constructor TSharedData.Create;
begin
  SetFPol(1);
end;

class function TSharedData.GetInstance: TSharedData;
begin
  TMonitor.Enter(Key); // <-- error here
  try
    if FUniqueInstance = nil then
    begin
      FUniqueInstance := TSharedData.Create;
    end;
  finally
    TMonitor.Exit(Key);
  end;
  Result := FUniqueInstance;
end;

procedure TSharedData.SetFPol(const Value: integer);
begin
  FPOL := Value;
end;

initialization
  Key:= TObject.Create;
finalization
  Key.Free;

I need now the same code in Delphi 7. But the compiler said, "TMonitor isn't known".
Where can I find TMonitor or how can I replace it with an alternative function?
I thank you in advance for any information.

A:

You can use TCriticalSection from SyncObjs unit.
The approach changes just a little bit. The critical section should be used as an object. So if you want to protect an area of you object on can do something like: 
type
  TSafeCounter = class(TObject)
  private
    FValue: Integer;
    FCriticalSection: TCriticalSection;
  public
    constructor Create;
    destructor Destroy; override;

    procedure SafeInc;
    procedure SafeDec;

    function CurValue: Integer;
  end;

implementation

{ TSafeCounter }

constructor TSafeCounter.Create;
begin
  FCriticalSection := TCriticalSection.Create;
end;

function TSafeCounter.CurValue: Integer;
begin
  FCriticalSection.Acquire;
  try
    Result := FValue;
  finally
    FCriticalSection.Release;
  end;
end;

procedure TSafeCounter.SafeDec;
begin
  FCriticalSection.Acquire;
  try
    Dec(FValue);
  finally
    FCriticalSection.Release;
  end;
end;

destructor TSafeCounter.Destroy;
begin
  FCriticalSection.Free;

  inherited;
end;

procedure TSafeCounter.SafeInc;
begin
  FCriticalSection.Acquire;
  try
    Inc(FValue);
  finally
    FCriticalSection.Release;
  end;
end;

If you are facing very extreme scenario (performance), you can work another kinds of implementations of critical sections, but them will also increase the complexity of working with it like the read/write critical section.

