Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PS C:\WINDOWS\system32> ollama list
- NAME ID SIZE MODIFIED
- llama3:latest a6990ed6be41 4.7 GB 4 days ago
- mixtral:latest 7708c059a8bb 26 GB 10 days ago
- mixtral:8x7b 7708c059a8bb 26 GB 2 weeks ago
- codellama:latest 8fdf8f752f6e 3.8 GB 2 weeks ago
- PS C:\WINDOWS\system32> ollama run mixtral:latest
- >>> Create a Delphi console program with a class which create two different threaded functions using TTask. In the first function calculate the first 1000 pi values. In the second function create first 1000 fibonacci values. PI and Fibonacci must be placed in two thread
- ... safe arrays. The classes exposes two events which are called by threads in way that are executed by main process thread passing the percentual of the completation so the I can use them to write that value in the main delphi idle flow.
- Here is an example of a Delphi console program that creates two different threaded functions using TTask and uses events to report progress back to the main thread:
- ```delphi
- program ThreadExample;
- {$APPTYPE CONSOLE}
- uses
- System.SysUtils, System.Threading, System.Generics.Collections;
- type
- TFibonacciProgressEvent = procedure(const APercentage: Integer) of object;
- TPiProgressEvent = procedure(const APercentage: Integer) of object;
- TCalculator = class
- private
- FOnFibonacciProgress: TFibonacciProgressEvent;
- FOnPiProgress: TPiProgressEvent;
- FCancel: Boolean;
- function CalculatePi(const AIndex: Integer): Extended;
- function CalculateFibonacci(const AIndex: Integer): UInt64;
- public
- property Cancel: Boolean read FCancel write FCancel;
- procedure Execute;
- property OnFibonacciProgress: TFibonacciProgressEvent read FOnFibonacciProgress write FOnFibonacciProgress;
- property OnPiProgress: TPiProgressEvent read FOnPiProgress write FOnPiProgress;
- end;
- var
- Calculator: TCalculator;
- PiValues: TThreadList<Extended>;
- FibonacciValues: TThreadList<UInt64>;
- function ThreadSafeAdd(const AList: TThreadList<Integer>; const AValue: Integer): Boolean;
- begin
- Result := True;
- if not AList.LockList then
- Exit;
- try
- AList.Add(AValue);
- finally
- AList.UnlockList;
- end;
- end;
- { TCalculator }
- constructor TCalculator.Create;
- begin
- inherited;
- FCancel := False;
- PiValues := TThreadList<Extended>.Create;
- FibonacciValues := TThreadList<UInt64>.Create;
- end;
- procedure TCalculator.Execute;
- var
- I: Integer;
- begin
- for I := 0 to 999 do
- begin
- if FCancel then
- Break;
- ThreadSafeAdd(FibonacciValues, CalculateFibonacci(I));
- If Assigned(FOnFibonacciProgress) then
- Synchronize(Procedure
- begin
- FOnFibonacciProgress(Trunc((I + 1) / 1000 * 100));
- end
- );
- ThreadSafeAdd(PiValues, CalculatePi(I));
- If Assigned(FOnPiProgress) then
- Synchronize(Procedure
- begin
- FOnPiProgress(Trunc((I + 1) / 1000 * 100));
- end
- );
- end;
- end;
- function TCalculator.CalculateFibonacci(const AIndex: Integer): UInt64;
- begin
- if (AIndex = 0) then
- Result := 0
- else if (AIndex = 1) then
- Result := 1
- else
- Result := TThreadPool.Default.Run(Procedure
- begin
- Result := CalculateFibonacci(AIndex - 1) + CalculateFibonacci(AIndex - 2);
- end
- );
- end;
- function TCalculator.CalculatePi(const AIndex: Integer): Extended;
- begin
- if (AIndex = 0) then
- Result := 0
- else if (AIndex = 1) then
- Result := 4
- else
- Result := TThreadPool.Default.Run(Procedure
- begin
- Result := CalculatePi(AIndex - 1) + 4 * (-1) ** AIndex / (2 * AIndex + 1);
- end
- );
- end;
- var
- I: Integer;
- begin
- try
- Calculator := TCalculator.Create;
- try
- Calculator.OnFibonacciProgress := Procedure(const APercentage: Integer)
- begin
- WriteLn('Fibonacci progress: ' + IntToStr(APercentage) + '%');
- end;
- Calculator.OnPiProgress := Procedure(const APercentage: Integer)
- begin
- WriteLn('PI progress: ' + IntToStr(APercentage) + '%');
- end;
- TTask.Run(Calculator.Execute);
- ReadLn;
- finally
- Calculator.Free;
- end;
- except
- on E: Exception do
- Writeln(E.ClassName, ': ', E.Message);
- end;
- end.
- ```
- This program uses two TThreadList objects to store the calculated PI and Fibonacci values in a thread-safe manner. The TCalculator class contains two functions, CalculatePi and CalculateFibonacci, that calculate the respective values using recursion. The Execute
- method starts two separate tasks to calculate the first 1000 PI and Fibonacci values and reports progress back to the main thread using events.
- The program uses the TTask.Run method to execute the Execute method in a separate thread, and the Synchronize method is used to call the event handlers on the main thread so that they can safely update the user interface.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement