3

I have the following scenario:

The program I am writing simulates multiple air networks. Now I have written it so that each simulation is in its own thread with its own data. This is so that there can be no contamination of data and each thread is complete multitasking safe. I have implemented delphi's native TThread class for each thread.

The program has one main thread. It then creates multiple sub threads for each network (my current test case has 6). But each sub thread also has 4 additional threads since its network has more than one layout. So in total I have 31 threads, but only 24 threads actively processing. All threads are created in suspended mode so that I can manually start them.

The computer I am running on is an i5 laptop so it has 4 threads (2 physical cores + 2 HT). In the production environment this will run on servers so they will have more processing power.

Now in the main program thread I have added a break point after the for loop which executes the threads. This doesn't trigger immediately. Watching the debug console of Delphi, it looks like it is only actively running 4 threads at a time. As soon as one exits, it starts another thread.

The simulations in the threads are difficult to optimize and require a few loops to finish the simulation. And each loop requires the previous loop's data. But each simulation is completely independent of the next so the program is an excellent candidate for threads and pipe lining.

Now my question is why does it only start 4 threads and not all the threads as the code specifies?

EDIT Added pieces of code

Main program

    for i := 0 to length(Solutions)-1 do
    begin
        Solutions[i].CreateWorkers;  //Setup threads 
    end;
    for i := 0 to length(Solutions)-1 do
    begin
        Solutions[i].Execute;   //start threads
    end;
end;

isSolversBusy := false;    //breaking point doesn't trigger here

1St level of threads

procedure cSolution.Execute;
var
i : integer;
lIsWorkerStillBusy : boolean;
begin
    lIsWorkerStillBusy := true;
   for i := 0 to length(Workers)-1 do
    begin
        Workers[i].Start;
    end;
    while (lIsWorkerStillBusy) do
    begin
        lIsWorkerStillBusy := false;
        for i := 0 to length(Workers)-1 do
        begin
            if Workers[i].IsCalculated = false then
            begin
                lIsWorkerStillBusy := true;
            end;
        end;
        sleep(100);
    end;
    FindBestNetwork;
    IsAllWorkersDone := true;
end;

2nd level of threads

procedure cWorker.Execute;
begin
    IsCalculated := false;
    Network.UpdateFlows;
    Network.SolveNetWork;  //main simulation work
    CalculateTotalPower;
    IsCalculated := true;
end;

EDIT 2

The reason I create them all suspended is because I store them in an array and before I start them I first have create the workers and their properties. I am simulating air network scenarios. Each solution is a different layout, while each worker is a different way of running that layout.

I have to first calculate all the worker's start properties before I start them all. In hind sight I could modify the code to do that in the thread. It currently happens in the main thread. The creation of the threads happens before the piece of code I pasted here.

The reason I keep them all in threads is that I need to evaluate the results of each thread afterwords.

13
  • 5
    Probably the answer to the question can be found in the code. Jul 21, 2014 at 10:07
  • 1
    I added pieces of code. I tried to only add pieces I think will be necessary to keep it short. Please tell me if you want some of the other pieces of code. Jul 21, 2014 at 10:21
  • 1
    Thanks, your edits saved your question! Jul 21, 2014 at 10:32
  • I though it was something to do with my 4 native threads on my pc Jul 21, 2014 at 10:55
  • 1
    @DavidHeffernan Yes I know that. What I am trying to say is what task based approach does, will be ideal for my scenario. I just had bad experiences with unsupported third party libraries in Delphi specifically. And I already see that OTL already doesn't support the 3 newest Delphi versions. Jul 21, 2014 at 12:21

1 Answer 1

14

This is your problem :

for i := 0 to length(Solutions)-1 do
begin
    Solutions[i].Execute;   //start threads
end;

This does not start the threads - this is executing the Execute method in the calling thread. In fact you were not running only 4 threads at a time, you were not even running one - all of this work would be done on the main thread sequentially. To resume a suspended thread you must use Solutions[i].Start.

The Execute method of a TThread is a special method that is executed on the worker thread created by the TThread automatically. When you create a TThread this method is automatically run on the worker thread that the TThread creates. If you create the thread suspended then it simply waits for you to wake the thread before beginning this work. Calling the .Start method of a TThread is what accomplishes this - triggering the underlying worker thread to begin executing the .Execute method.

Otherwise, the Execute method, and all other methods of a TThread are no different from any other normal method belonging to a class. They can be executed on any thread that calls them directly.

In this case, it doesn't seem like you are doing any additional work in the main thread between creating and executing your workers. In this case, unless you have an explicit need for it, you could simply create your threads not-suspended and let them execute automatically upon creation.

7
  • Okay, so in place of execute I should just call resume and it will "resume" the thread at execute? Jul 21, 2014 at 10:25
  • @blackwolfsa My mistake - Resume is deprecated. You should use .Start instead, but yes, otherwise this is what you need to do.
    – J...
    Jul 21, 2014 at 10:26
  • 1
    Actually, I don't understand why you create the threads suspended at all. That seems rather pointless. Why create a bunch of threads suspended, and then immediately start them? Jul 21, 2014 at 10:33
  • @DavidHeffernan Agreed, I wondered that too. I'll add the suggestion to my answer.
    – J...
    Jul 21, 2014 at 10:42
  • 1
    @DavidHeffernan Maybe he wants to run the threads simultaneously (for an unstated reason - and also as a bad practice), and the initialization takes a lot of time? - Again, I'm just guessing. (This might also be the product of some copy-paste programming.)
    – mg30rg
    Jul 21, 2014 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.