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.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,9b794838d82ee7c3,start X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.germany.com!news.motzarella.org!motzarella.org!not-for-mail From: =?ISO-8859-1?Q?S=E9bastien?= Newsgroups: comp.lang.ada Subject: Task vs Protected Type Date: Fri, 30 May 2008 14:12:49 +0000 Organization: A noiseless patient Spider Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: feeder.motzarella.org U2FsdGVkX1+K5Af20sWso02IFl0XNvOlqP1NrlA5hDyom3ReF960BNOD6EgHAIJkY7UIEOxU/jLG4u/AWf2XvdWn9uY7GD1xM8A2tFoGnejHbHJJipEN+UTXW2X306+SX88WqQ0IVzlJ+bpBYVMFEw== X-Complaints-To: Please send complaints to abuse@motzarella.org with full headers NNTP-Posting-Date: Fri, 30 May 2008 14:12:52 +0000 (UTC) X-Auth-Sender: U2FsdGVkX18M9b35hz4NpuFIbEHN39mjPLEEGvR7Q2McfBxarwT7bQ== Cancel-Lock: sha1:pNQDhm9e9LJHPFkrxg172eqT4lQ= User-Agent: Thunderbird 2.0.0.14 (Windows/20080421) Xref: g2news1.google.com comp.lang.ada:478 Date: 2008-05-30T14:12:49+00:00 List-Id: Hi guys, I would like to understand a sentence of the wikibook. In the presentation of Task and Protected we can see the following: ------------------- -- CODE 1 ------------------- task type Semaphore_Task_Type is entry Initialize (N : in Natural); entry Wait; entry Signal; end Semaphore_Task_Type; ... task body Semaphore_Task_Type is Count : Natural; begin accept Initialize (N : in Natural) do Count := N; end Initialize; loop select when Count > 0 => accept Wait do Count := Count - 1; end Wait; or accept Signal; Count := Count + 1; end select; end loop; end Semaphore_Task_Type; ------------------- -- CODE 2 ------------------- protected type Semaphore_Protected_Type is procedure Initialize (N : in Natural); entry Wait; procedure Signal; private Count : Natural := 0; end Semaphore_Protected_Type; ... protected body Semaphore_Protected_Type is procedure Initialize (N : in Natural) is begin Count := N; end Initialize; entry Wait when Count > 0 is begin Count := Count - 1; end Wait; procedure Signal is begin Count := Count + 1; end Signal; end Semaphore_Protected_Type; Ok the two codes provides the same functionality but obviously not in the same way. In the first one, a thread is dedicated to manage the semaphore. In the second one, this is a protected type with exclusive mutex on entry and procedure and absolutly no thread implementation (as far as I understood internal mechanism of protected type) The first one has the advantage to force the user to Initialize the task because of the first entry, but there is no such mechanism in the protected type. So in the wikibook, there is this sentence: "Alternatively, semaphore functionality can be provided by a protected object, with major efficiency gains." This is between the two codes, letting the reader (me at least) understand protected type is a better approach, but I can't understand why. For example, you have a web application and want to develop an object pool. You could use one of both example, since it's the same purpose with some GetObject and ReleaseObject enties/procedure in a task or in a protected. But which one is the better approach? Thanks very much. Sebastien