From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,146880aa5d8c64c7 X-Google-Attributes: gid103376,public From: Mats Weber Subject: Re: Array of tasks Date: 1997/12/10 Message-ID: <348EAF76.4C895866@elca-matrix.ch>#1/1 X-Deja-AN: 296928215 References: <348D7832.27A4@assist.org> <348DC3C0.7555@gsfc.nasa.gov> <348DD9B5.11CD@assist.org> Reply-To: Mats.Weber@elca-matrix.ch Organization: ELCA Matrix SA Newsgroups: comp.lang.ada Date: 1997-12-10T00:00:00+00:00 List-Id: 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.