troubleshooting Question

Delphi Threads timing don't add up

Avatar of ivanxerone
ivanxerone asked on
Delphi
17 Comments1 Solution628 ViewsLast Modified:
Here is some sample code in Delphi (D7), a single form with:
1 tspinedit, 1 tmemo, 1 tbutton, 1 TJvMTManager, and 1 TJvMTThread
//========================================================
procedure TForm1.JvMTThread1Execute(Sender: TJvMTThread; MTThread: TJvMTSingleThread);
Var X:integer;
    ST:STRING;
    STARTTIME:LONGINT;
begin
   starttime:=gettickcount;
   FOR X:=1 TO 100000 DO BEGIN
      //if (x mod 10)=0 then
                  application.processmessages;      // doesnt make a difference in the results
      st:='test';   //test code
      st:=ansiuppercase(st);  //test code
      st:=ansilowercase(st);  //test code
      END;
   STARTTIME:=gettickcount-starttime;
   memo1.lines.add(format('FINISHED (%dms)',[starttime]));
end;
//========================================================
procedure TForm1.Button1Click(Sender: TObject);
var x:integer;
begin
memo1.clear;
for x:=1 to SpinEdit1.value do
    JvMTThread1.RunCopy;
end;
//========================================================

I found the issue with TThread, and then I got the same results with TJvMTThread.
The issue is shown in the results:
      1 single thread runs at ~200ms
      2 threads results in ~2600ms (more than 6 times of what it should be?)
      4 threads ~5500 ms

In no moment CPU usage goes over %50, even when setting thread priority to real_time.
Now, those numbers could be lowered significantly by using a criticalsection.enter / leave (For instance, 2 threads should run at ~400ms) but that's not the way threads should work.

The way I want threads to work is exactly the same way as when executing this application twice and running it in sync, resulting them both running at ~200ms.

Something else about threads I don't know?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 17 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 17 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros