source30 EEMOEZIYHBMKQNDTTKJDAP5UZYYLVFI2D4W3LSA








Source30


unit PrimeRangeThread;

interface

uses
Classes, BlockToAsyncBuf;

type
TPrimeRangeThread = class(TThread)
private
{ Private declarations }
FBuf: TBlockToAsyncBuf;
protected
function IsPrime(TestNum: integer): boolean;
procedure Execute; override;
public
published
property Buf: TBlockToAsyncBuf read FBuf write FBuf;
end;

TRangeRequestType = record
Low, High: integer;
end;

PRangeRequestType = ^TRangeRequestType;

{ Results returned in a string list }

implementation

uses SysUtils;

{ TPrimeRangeThread }

function TPrimeRangeThread.IsPrime(TestNum: integer): boolean;

var
iter: integer;

begin
result := true;
if TestNum < 0 then
result := false;
if TestNum <= 2 then
exit;
iter := 2;
while (iter < TestNum) and (not terminated) do {Line A}
begin
if (TestNum mod iter) = 0 then
begin
result := false;
exit;
end;
Inc(iter);
end;
end;

procedure TPrimeRangeThread.Execute;

var
PRange: PRangeRequestType;
TestNum: integer;
Results: TStringList;

begin
while not Terminated do
begin
PRange := PRangeRequestType(FBuf.BlockingRead);
if Assigned(PRange) then
begin
Assert(PRange.Low <= PRange.High);
Results := TStringList.Create;
Results.Add('Primes from: ' + IntToStr(PRange.Low) +
' to: ' + IntToStr(PRange.High));
for TestNum := PRange.Low to PRange.High do
begin
if IsPrime(TestNum) then
Results.Add(IntToStr(TestNum) + ' is prime.');
end;
if not FBuf.BlockingWrite(Results) then
begin
Results.Free;
Terminate;
end;
end
else Terminate;
end;
end;

end.






Wyszukiwarka

Podobne podstrony:
Matrix3?pp source
Thread?pp source
arm biquad ?scade ?1 ?st q31? source
arm conv ?2? source
arm mat mult q15? source
Resource 8inl source
arm fir lattice init q31? source
arm fir ?cimate ?st q15? source
source11
arm correlate ?st q15? source
connector?s source
source8
register? source
passing values source
arm iir lattice init ?2? source
arm mult q31? source
2004 11 Porównanie serwerów relacyjnych baz danych Open Source [Bazy Danych]

więcej podobnych podstron