comp.lang.ada
 help / color / mirror / Atom feed
From: Mats Weber <Mats.Weber@elca-matrix.ch>
Subject: Re: Array of tasks
Date: 1997/12/10
Date: 1997-12-10T00:00:00+00:00	[thread overview]
Message-ID: <348EAF76.4C895866@elca-matrix.ch> (raw)
In-Reply-To: 348DD9B5.11CD@assist.org


Larry Coon wrote:

> Okay, to be a little more complete -- My tasks are each assigned a task
> number, and I use a discriminant with a default to do it.  Here's my
> code again, this time with the default stuff implemented:

Here is how I would do it, see comments in the code for explanations:

--
with text_io;

use text_io;

procedure try_task_and_semaphore is

  package int_text_io is new text_io.integer_io(integer);

  -- Use a function to get the number of tasks, so you don't need
  -- a nested declare bock.
   
  function get_int (prompt : string) return integer is
    result : integer;
  begin
    put(prompt);
    int_text_io.get(result);
    return result;
  end;

  task_count: positive := get_int(prompt => "Enter number of tasks: ");

  subtype task_range is integer range 1..task_count;

  protected type semaphore is
    entry seize;
    procedure release;
  private
    available:  boolean := true;
  end semaphore;

  protected body semaphore is separate;

  semaphore_array: array (task_range) of semaphore;

  -- Counter does not need (and cannot, because the function get_next is 
  -- not allowed to modify data) be a protected type. See why in the 
  -- Ada 95 Rationale section 9.6. I changed it to a package.

  package counter is
    function get_next return integer;
  private
    data:   integer := 1;
  end counter;

  package body counter is
    function get_next return integer is
      return_val: integer;
    begin
      return_val        := data;
      data              := data + 1;
      return return_val;
    end get_next;
  end counter;

  task type t (task_no: integer := counter.get_next);
  task body t is separate;
  
  task_array: array (task_range) of t;

  -- With everything declared at the same level, the code is simpler
  -- (except for the function to get an integer) and you don't need
  -- access discriminants or other complicated stuff.

  -- Of course if you really need your tasks type to be parameterizable,
  -- then use access discriminants, but I wouldn't use them for visibility
  -- control.

begin
  null;
end;

-- separate bodies not changed.




  reply	other threads:[~1997-12-10  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-12-09  0:00 Array of tasks Larry Coon
1997-12-09  0:00 ` Stephen Leake
1997-12-09  0:00   ` Larry Coon
1997-12-10  0:00     ` Mats Weber [this message]
1997-12-10  0:00 ` Jean-Pierre Rosen
1997-12-10  0:00 ` Anonymous
replies disabled

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