I am trying to make a multithreaded random number generator for the command line interface, and for that I use TThreads to use multiple cpu cores to make random numbers. My program works till I try to start a thread, as it seemingly starts but, as far as I can tell, doesn't execute its code, because then my loop in line 196 should end, but it doesn't.
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
Classes,
sysutils,
math;
type
TMyThread = class(TThread)
private
procedure ShowStatus;
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: boolean);
end;
var
nums, temp1: int64;
f: tfilestream;
split_char: char;
commands: array[0..2] of int64;
writing: boolean = false;
done: array of boolean;
constructor TMyThread.Create(CreateSuspended: boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TMyThread.ShowStatus;
begin
done[temp1]:= false;
end;
procedure TMyThread.Execute;
var
s, temp: string;
i: uint64;
begin
Synchronize(@Showstatus);
i:= temp1;
s:= '';
while not nums < 0 do
begin
writeln('test.................................');
dec(nums);
temp:= inttostr(randomrange(commands[1], commands[2])) + split_char;
if high(s) + high(temp) > 4096 then
begin
if not writing then
begin
writing:= true;
f.Write(s[1], high(s));
s:= temp;
writing:= false;
end;
end
else
s:= s + temp;
end;
done[i]:= true;
end;
var
i, timestamp, timestamp2: uint64;
temp, cores: int64;
f_temp, temp_s: string;
close, time, written, writeable, file_specified, numbers_specified, range_specified, split_char_specified: boolean;
is_finished: boolean = false;
threads: array of TMyThread;
label
jump;
begin
timestamp:= gettickcount64;
time:= false;
writeable:= false;
written:= false;
file_specified:= false;
numbers_specified:= false;
range_specified:= false;
split_char_specified:= false;
cores:= 0;
for i:= 1 to paramcount() do
try
case paramstr(i) of
'/f', '/file': begin
file_specified:= true;
f_temp:= paramstr(i + 1).split('"')[0];
if fileexists(f_temp) then
deletefile(f_temp);
f:= tfilestream.create(f_temp, fmcreate);
writeable:= true;
end;
'/n', '/numbers': begin
numbers_specified:= true;
commands[0]:= strtoint(paramstr(i + 1)) - 1;
end;
'/r', '/range': begin
range_specified:= true;
commands[1]:= strtoint((paramstr(i + 1).split(';'))[0]);
commands[2]:= strtoint((paramstr(i + 1).split(';'))[1]) + 1;
end;
'/s', '/split', 'splitchar': begin
split_char_specified:= true;
split_char:=paramstr(i + 1)[1];
end;
'/t', '/time': time:= true;
'/tr', '/truerand': randomize;
'/c', '/cores': cores:= strtoint(paramstr(i + 1));
end;
except
if not writeable then
begin
write('cannot write to file');
written:= true;
end;
close:= true;
end;
if not file_specified then
begin
write('file not specified');
written:= true;
close:= true;
end;
if numbers_specified then
begin
if commands[0] < 1 then
begin
if written then
write(#13);
write('cannot generate <1 numbers');
written:= true;
close:= true;
end;
end
else
begin
if written then
write(#13);
write('amount of numbers not specified');
written:= true;
close:= true;
end;
if not range_specified then
begin
if written then
write(#13);
write('range not specified');
written:= true;
close:= true;
end
else
if commands[1] = commands[2] then
begin
temp:= commands[1];
commands[2]:= commands[1];
commands[2]:= temp;
end;
if not split_char_specified then
begin
if written then
write(#13);
write('split-char not specified');
close:= true;
end;
if close then
halt;
try
if (cores = 0) or (cores > getcpucount) then
cores:= getcpucount;
setlength(threads, cores);
setlength(done, cores);
for i:= low(threads) to high(threads) do
threads[i]:= default(Tmythread);
nums:= commands[0];
writeln(inttostr(length(threads)));
writeln('test0');
writeln('threadcount: ' + inttostr(length(threads)));
for i:= low(threads) to high(threads) do
begin
writeln('test?');
threads[i].create(true);
writeln('thread ' + inttostr(i) + ' initialized');
temp1:= i;
done[temp1]:= true;
while done[temp1] = true do
begin
writeln('waiting for thread to start');
end;
end;
writeln('test1');
while not is_finished do
begin
i:= 0;
jump:
if not done[i] then
goto jump;
inc(i);
if i = high(done) then
is_finished:= true;
end;
temp_s:= inttostr(randomrange(commands[1], commands[2])) + split_char;
f.Write(temp_s[1], high(temp_s));
except
on e: exception do
begin
write(#13 + e.message);
deletefile(paramstr(i));
write('ERROR! Program will exit');
exit;
end;
end;
f.free;
if time then
begin
if written then
write(#13);
write(floattostr((gettickcount64 - timestamp) / 1000) + ' seconds to execute');
end;
end.
CreateSuspended
literally means just that - didn't you ever wonder about that?