There's also Parallel procedures package but its usage is unnatural for me, e.g. the parameters must be passed as pointer, etc.
MultiThreadProc is a very handy way to setup multithreading.
1. install and add MtProcs to your application's dependency list
2. uses MtProcs...
3. create a class with data to process and a method run
4. call MtProcs run function
type
TSingleOperation=class
FDataA,FDataB : byte;
procedure Run;
end;
TMaster=class(TSomething)
FProcessing:TList;
procedure RunMTProc(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
procedure SetupProcessing;
procedure StartProcessing;
end;
constructor TMaster.Create;
begin
inherited;
FProcessing:=TList.Create;
end;
procedure TMaster.SetupProcessing;
var
aOperation:TSingleOperation;
begin
aOperation:=TSingleOperation.Create;
aOperation.A:=1; aOperation.B:=2;
FProcessing.Add(aOperation);
end;
procedure TMaster.StartProcessing;
begin
if DoRunMultiThreaded then //some flag to demonstrate running either by mtprocs or sequential
ProcThreadPool.DoParallel(@RunMTProc,0,FProcessing.Count-1) else
for i:=0 to FProcessing.Count-1 do
TSingleOperation(FProcessing[i]).Run;
end;
procedure TMaster.RunMTProc(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
begin
TSingleOperation(FProcessing[Index]).Run;
end;
procedure TSingleOperation.Run;
begin
Result:=A+B;
end;Take care about synchronizing data with Synchronize() or Enter/LeaveCriticalSection().