I have a procedure with tasks in it. I have to do something after the all of the tasks terminated. How can I do that?
Asked
Active
Viewed 2,606 times
1
-
1The nature of this question suggests that you would be better off providing more info about what functionality you're trying to implement, rather than asking how to do this specific thing. The reason is that what you're asking isn't typically something an Ada programmer would find themselves dealing with. – Marc C Dec 11 '10 at 21:36
2 Answers
7
Declare the tasks in an inner block: the block won't exit until all the tasks are complete, ARM7.6.1(4)
with Ada.Text_IO; use Ada.Text_IO;
procedure After_Tasks is
begin
Put_Line ("at the start");
declare
task T1;
task T2;
task body T1 is
begin
delay 1.0;
Put_Line ("t1 done");
end T1;
task body T2 is
begin
delay 2.0;
Put_Line ("t2 done");
end T2;
begin
null;
end; -- block here until T1 & T2 are completed
Put_Line ("at the end");
end After_Tasks;

Simon Wright
- 25,108
- 2
- 35
- 62
-
This would be my suggestion as well. However, I'd probably just put the "at the end" code in the procedure's calling code right after the call to the procedure. Declare blocks always look like a hack to me. – T.E.D. Dec 22 '10 at 13:56
-
I declare the task on the heap: `One_Test_Task := new Test_Task();` This doesn't seem to work then. – isgoed Jul 16 '21 at 11:50
-
@isgoed, the link I gave is to some deep stuff about _masters_. If you declare the access type in the procedure or declare block, it works as I said. If you declare it further up, that place then becomes the master for the allocated tasks. – Simon Wright Jul 16 '21 at 17:09
5
Without any knowledge of what you're actually trying to accomplish, a couple stabs at accomplishing this would be:
- Monitor (poll) each pending task's 'Terminated attribute.
- Implement a "Shutdown" entry in your task(s) that is the last thing each task performs. Have your "controller" rendezvous with each task's Shutdown entry and once all tasks have accepted and completed the rendezvous, for all intents and purposes you can conclude that the tasks have all terminated. For the pedantic among us, we might execute a short delay (
delay 0.0;
) and then verify via the 'Terminated attribute that all tasks are terminated, or at leastpragma Assert()
so.

Marc C
- 8,664
- 1
- 24
- 29