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=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.uzoreto.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: How to terminate all running tasks? Date: Wed, 10 Jun 2020 15:12:15 +0300 Organization: Tidorum Ltd Message-ID: References: <7c14755d-f5f0-4000-bde1-d34423cf43abo@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net 1E1PrdxD5fbKY3DDPWkEuw//h78ELdgzbVzs+9yekleduazBQy Cancel-Lock: sha1:lt++0j7MfIJgEZQ0XDq2tLMT9Kc= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 In-Reply-To: <7c14755d-f5f0-4000-bde1-d34423cf43abo@googlegroups.com> Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:59032 Date: 2020-06-10T15:12:15+03:00 List-Id: On 2020-06-10 14:14, Gilbert Gosseyn wrote: > Hi, It would be easier to understand your post if you started with the explanation and question, rather than throwing a bunch of uncommented code at the reader. > -- package spezification > package packp6 is > procedure tp6pm(N : Long_Integer); > end packp6; > > -- package body > with Ada.Text_IO; use Ada.Text_IO; > with Ada.Task_Identification; > package body packp6 is > procedure tp6pm(N : Long_Integer) is > use Ada.Task_Identification; > package LIO is new Integer_IO(Long_Integer); > solution_found : exception; > > task p6p; > task p6m; > > task body p6p is > pp,i : Long_Integer := 0; > begin > loop > i := i+1; > pp := 6*i+1; > if N mod pp = 0 then > new_line;put("pp= ");LIO.put(pp); > raise solution_found; > end if; > end loop; This is the last point at which you can handle the "raise" above. If there is no handler here, WITHIN task p6p, the exception will try to propagate out of the task, which will terminate the task and STOP the propagation of the exception. > end p6p; > > task body p6m is > pm,i : Long_Integer := 0; > begin > loop > i := i+1; > pm := 6*i-1; > if N mod pm = 0 then > new_line;put("pm= ");LIO.put(pm); > raise solution_found; > end if; > end loop; Same comment as above. > end p6m; > begin > null; > exception > when solution_found => This handler is never entered, because the "null" statement above does not raise solution_found. > Abort_Task(p6p'Identity); > Abort_Task(p6m'Identity); > end tp6pm; > end packp6; [snip] > When in a task the exception solution_found is raised, then I want > all running tasks to be terminated immediately. It is not possible to use an exception, raised in a task, to signal something outside the task in that way. > Apparently this does not happen. How to improve? You must use some other way to inform the main subprogram that a solution has been found. There are may ways, but for example you can use an synchronization object as follows: with Ada.Synchronous_Task_Control; ... solution_found : Ada.Synchronous_Task_Control.Suspension_Object; ... task body p6p ... Ada.Synchronous_Task_Control.Set_True (solution_found); -- Instead of the "raise". ... same for task p6m and in the main subprogram, instead of the null statement and the exception handler: Ada.Synchronous_Task_Control.Suspend_Until_True (solution_found); abort p6p; abort p6m; -- Niklas Holsti niklas holsti tidorum fi . @ .