Delphi CPU Usage low during MultiThreading

2019-03-05 13:57发布

问题:

During Executing of a Multithreading program I see 8 threads starting in the Delphi Event log.

(My CPU is a Intel 7 with 4 cores HyperThreaded so 8 Calculation Cores) but in my TaskManager at the Performance tab I see only 12% CPU usage and only one core calculating with performance up to about 70 - 80 %. Did compile my multithreading program with OTL usage and with ParallelFor usage, But still only 12% performance and only One core doing the work.

On my Form1 I have a ButtonClick procedure with the OTL parallel.ForeEach which iterates over items of a StingList. The StringList lines contains each a Name, a Path to a differend DataFile and a DataFormat of the File. The ForEach.execute() Starts a 'EntrySearch'procedure on other Unit, The EntrySearch procedure starts with the extraction of the info from the appropiate line of the Stringlist. In a 'While X < Y loop' is the data extracted from the DataFile through a AssignFile and While not eof,read the lines with data. Calcuclations are made on the Data until the 'While X < Y' loop ends

I can see that 8 (CPUcount) Threads are started at the ButtonClick procedure. In the TaskManager I see only one CPU core start working total of about 12% ProcessorUsage. When after the calculations the ProcessorUsage returns to 0% the .exe program is hanging and I have no controlover the program. From the little data I can extract out of the CalculationUnit I getonly data from the last started thread, as of this last thread makes the other threads stop and cannot make their caculations and can not terminate.

{the OTL in the ButtonClick procedure}
       Parallel.ForEach(0, StrList.Count-1)
       .PreserveOrder
       .NumTasks(CPUCount)
       .NoWait
       .Execute(
        procedure(const value: integer)
         begin
           CalcUnit.EntrySearch(value);
         end);

    {procedure on CalcUnit} 
    procedure EntrySearch(value: integer);
    begin 
     {extract Name, Path DataFile and DataFormat from StringList}
      While X < Y do begin
        AssignFile(qMSInputFile7, Path);
        {$I-} reset(qMSInputFile7); {$I+}
        While Not eof(qMSInputFile7) do Begin
          with qMetaRec7 do begin
            Read (qMSInputFile7, qMetaRec7);
             { Extract the Data}
           end; // While not eof
        {Make calculations}   
      end; // While X<Y 
    end;

What goes wrong? and how can I solve this. Thanks A lot.