Search code examples
multithreadingdelphiomnithreadlibrary

Delphi OTL Why does MultiThreading program uses only half the available CPU's


During executing my multithreading program only 4 of the available 8 CPU's are being used. Why? What can I change to make all CPU's working?

  Parallel.ForEach(0, CalcList.Count-1)
  .NumTasks(nMax)
  .NoWait
  .Execute(
    procedure(const value: integer)
    begin
      CalcUnit.EntrySearch(value);
    end);

(nMax and the CalcList.Count are both 16, Intel I7 HyperThreaded)

Thank you


Solution

  • I just did a test on an i7 2600 (4 cores 8 HT) using OTL. A simple Parallel.ForEach loop makes use of all 8. With and without the .NumTasks that you have. There is no problem with the library.

    begin
      Parallel.ForEach(0, 100)
      //.NumTasks(16)
      .Execute(
        procedure(const value: integer)
        var
          newValue: Single;
          I: Integer;
        begin
          newValue := value;
          for I := 1 to 100000000 do
          begin
            newValue := newValue * I;
            newValue := newValue / I;
          end;
        end);
      ShowMessage('Done!');
    end;
    

    My guess is that the problem is in your code. Disk accesses in threads are a good way to counter the benefits of using threads in the first place.

    I don't know enough about your code but you should rather look at reading in the data in a single thread and then threading the actual processing of that data.

    I see that you also have .NoWait specified. Are you saving the return value for your Parallel.ForEach? Its a good idea to save this value because otherwise your code will block when the OnClick exits. See gabr's answer to this question.

    Why is OmniThreadLibrary's ForEach blocking main thread?