comp.lang.ada
 help / color / mirror / Atom feed
From: mockturtle <framefritti@gmail.com>
Subject: Re: dynamic vs static tasks allocation
Date: Thu, 16 Oct 2014 11:18:40 -0700 (PDT)
Date: 2014-10-16T11:18:40-07:00	[thread overview]
Message-ID: <cb700ad1-1cb7-44e9-934d-c1580c2401c2@googlegroups.com> (raw)
In-Reply-To: <a4e161b4-f966-4c40-80b9-6dfe760815da@googlegroups.com>

On Thursday, October 16, 2014 7:25:51 PM UTC+2, Stribor40 wrote:
> If you know ahead of time how many tasks will be needed is it better to create tasks dynamically with new operator or just statically?
>  
> Which is way is recommended and why even bother allocating dynamically?

Nice question... I guess that the answer is a definitive "it depends."  

Just my 2.0e-2... 

As a general rule, I always prefer to avoid dynamic allocation, so I prefer to allocate the task statically.  BTW, please note that if you do something like

   task type Foo;

   declare
     Worker : Foo; -- Worker starts here
   begin
     -- Worker is running
     ... do something ...
   end;

task Worker is started "dynamically" when the execution reaches the "declare" block, without using "new."  I do not know if your idea of "dynamically" includes this example or not.  Also note that in this case

   procedure Bar(N: Positive) is
     Workers : array (1..N) of Foo;  
   begin
      --  N workers running
      ... do something;
   end Bar;

the number of tasks is determined dynamically at runtime.


Another reason for having static tasks is (in general) efficiency.  The typical example is a web server that waits for connections on the port 80 and every time a connection arrives, it hands the new connection to a sub-server that takes care of all the dialogue with the client.  In this case you have two possible macro-approaches: create the sub-server task at runtime with "new" or keep a "pool" of static sub-servers that serve the requests, then go back to sleep, waiting for a new request.  I expect the second solution to be more efficient (in terms of time required to reply to the client) since you avoid the overhead related with task creation.  (We are  on the border of the sin of "preventive optimization" here, a more precise analysis should be done on a case-by-case basis).

Moreover, if I remember correctly, there are some "profiles" that do not allow for the dynamic creation of tasks, so all your tasks must be "static."

Finally, why using "new" for creating new tasks?  Well, once I needed to keep a task "inside" a record, but if you declare a component of task type the record will be limited (it does not make any sense to copy a record).  Since having the record limited was a problem, I used an access to task and this required to have the task created dynamically.  (Sorry, I do not remember the details, it was too much time ago).

Hope this helps.

Riccardo

  reply	other threads:[~2014-10-16 18:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-16 17:25 dynamic vs static tasks allocation Stribor40
2014-10-16 18:18 ` mockturtle [this message]
2014-10-16 18:41   ` Adam Beneschan
2014-10-16 18:45 ` Jeffrey Carter
2014-10-17  0:07 ` Dennis Lee Bieber
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox