IROThreadPoolCallback

Overview

The IROThreadPoolCallback interface provides asynchronous event processing via a callback mechanism. It allows to decouple event discovering threads from event processing threads.

Use case

The following example demonstrates the usage of the IROThreadPoolCallback interface:

program ThreadPoolCallbackDemo;

{$APPTYPE CONSOLE}

uses
  Classes, SysUtils,
  uROThreadPool;

type
  TClockReporter = class (TInterfacedObject, IROThreadPoolCallback)
    fID   : integer;
  protected
    procedure Callback(Caller: TROThreadPool; aThread: TThread);
  public
    constructor Create (id : integer);
  end;

{ TWorkerThread }

constructor TClockReporter.Create(id: integer);
begin
  inherited Create;
  fID := id;
end;

procedure TClockReporter.Callback(Caller: TROThreadPool; aThread: TThread);
begin
  WriteLn (fID, ' (', aThread.ThreadID, '): ', DateTimeToStr (Now));
end;

procedure DoDemo;
const
  LOOP_COUNT    = 10;
  REPORTER_COUT = 3;
var
  threadPool    : TROThreadPool;
  reporters     : array [1..REPORTER_COUT] of IROThreadPoolCallback;
  i, j          : integer;
begin
  threadPool := TROThreadPool.Create(nil);

  for j := 1 to REPORTER_COUT do
    reporters [j] := TClockReporter.Create (j);

  for i := 1 to LOOP_COUNT  do begin
    Sleep (1000);
    for j := 1 to REPORTER_COUT do
      if Random > 0.5 then
        threadPool.QueueItem (reporters [j]);
  end;

  threadPool.Free;
end;

begin
  try
    DoDemo;
  except on E : Exception do
    WriteLn ('Exception: ' + E.Message);
  end;
end.

Location


Required Methods


Callback

Notifies of an event.

procedure Callback(Caller: TROThreadPool; aThread: TThread)

Parameters:

  • Caller: Event queue and thread manager
  • aThread: Working thread to process the event