TSpinWait (Delphi)
Description
The following example illustrates the usage of the TSpinWait record. This is a console application, where the main thread waits in a spin-loop until a global variable is set by a secondary thread.
Code
program TSpinWait_example;
{$APPTYPE CONSOLE}
uses
  SysUtils, SyncObjs, Classes;
var
  Flag: Boolean;
type
  TThreadCause = class(TThread)
  private
    procedure Execute; override;
  end;
procedure TThreadCause.Execute;
begin
  Sleep(100); { 100 milliseconds }
  Flag := True;
end;
var
  LCause: TThreadCause;
  LSpinner: TSpinWait;
begin
  Flag := False;
  LCause := TThreadCause.Create(True);
  LCause.Start;
  LSpinner.Reset;
  while Flag = False do
  begin
    Writeln(IntToStr(LSpinner.Count));
    LSpinner.SpinCycle;
  end;
  Writeln(IntToStr(LSpinner.Count));
  Writeln(Flag); { displays TRUE }
end. { Put breakpoint here to see the console output. }
Uses
- SysUtils.TSpinWait ( fr | de | ja )
- SysUtils.TSpinWait.SpinCycle ( fr | de | ja )