I had to go back to a simple threading example I gave a couple of weeks ago, and while testing some code on an arguably much slower processor I got results that are inexplainable to me: a windows console running the app is thousands, times slower than running the same code on Linux running under wsl2 (hyperv'd) on the same machine (or for that matter a raspberry pi 3/4/5/).
Anyone of you know how to explain the Windows console is so much slower? (file IO is OK)
program simplethread2;
{$mode objfpc}{$I-}
{$modeswitch anonymousfunctions}
uses {$ifdef unix}cthreads,{$endif}sysutils,classes;
var
Sync:IReadWriteSync;
WorkerThread1,
WorkerThread2,
ControllerThread:TThread;
start:int64;
begin
Sync:=TMultiReadExclusiveWriteSynchronizer.create;
Sync.BeginWrite;
start := gettickcount64;
Sync.EndWrite;
writeln('Program start time saved');
WorkerThread1:=TThread.executeinthread(procedure
var
s:string = 'Hey Main, I am still busy....';
i:integer;
begin
for i := 0 to 100000 do if i mod 100 = 0 then
begin
Sync.BeginWrite;
writeln(s);
Sync.EndWrite;
end;
Sync.BeginRead;
writeln('WorkerThread1 done', ' took:', gettickcount64 - start);
Sync.EndRead;
end);
WorkerThread2:=TThread.executeinthread(procedure
var
s:string = 'Hey, One,Still chatting?....';
i:integer;
begin
for i := 0 to 100000 do if i mod 100 = 0 then
begin
Sync.BeginWrite;
writeln(s);
Sync.EndWrite;
end;
Sync.BeginRead;
writeln('WorkerThread2 done', ' took:', gettickcount64 - start);
Sync.EndRead;
end);
ControllerThread:=TThread.executeinthread(procedure
begin
writeln('Test the ControllerThread');
WorkerThread1.waitfor;
WorkerThread2.waitfor;
Sync.BeginRead;
writeln('ControllerThread done, handing over to main', ' took:', gettickcount64 - start);
Sync.EndRead;
end);
writeln('perform main workload');
//sleep(10000);
writeln('Main workload finished', ' took:', gettickcount64 - start);
if not ControllerThread.finished then ControllerThread.waitfor;
writeln('Main thread finished in ', gettickcount64 - start);
end.
The sync code is superfluous, you can ignore that. That is rediculous.
Without the console IO the code runs at about the same speed. (and yes, I already applied the {$I-} state.)
Ignore the outputs, that is as expected. In your own tests you can see that the threading is working, just scroll up.