Reputation: 101
Is there an way To run an Ada Task in Backround ?
package body Main is task type Background is end Background; task body Background is begin loop delay 1.0; Do_Stuff; end loop; end Background; procedure Enable is B_TASK:Background; begin --Please do not wait here.. null; end Enable; end Main;
Upvotes: 1
Views: 155
Reputation: 2142
Your question is a little vague (the meaning of "background" is unclear), but from the code example it seems you want a procedure Enable such that when it is called, a new "Background" task is started and Enable can return to its caller without waiting for the new task to terminate, leaving the new task running concurrently with the original task that called Enable.
You can create such "background" tasks by creating them dynamically, using an access type and the "new" keyword:
type Background_Ref is access Background;
procedure Enable
is
B : Background_Ref := new Background;
-- A new Background task is created and starts running
-- concurrently with the present task.
begin
null;
-- This does not wait for the new task to terminate.
end Enable;
Note that in this case the program itself will wait for all created background tasks to terminate, before the whole program terminates.
Upvotes: 4
Reputation: 5021
You have not described what you mean by "background".
Here is a simple Ada program where the "background" task runs as I think you want a background task to run.
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
task background;
task body background is
begin
for I in 1 .. 10 loop
Put_Line ("Background:" & I'Image);
delay 1.5;
end loop;
end background;
begin
for I in 1 .. 10 loop
Put_Line ("Main" & I'Image);
delay 1.0;
end loop;
end Main;
A sample output from this program is:
Main 1
Background: 1
Main 2
Background: 2
Main 3
Background: 3
Main 4
Main 5
Background: 4
Main 6
Background: 5
Main 7
Main 8
Background: 6
Main 9
Background: 7
Main 10
Background: 8
Background: 9
Background: 10
The main task clearly works independently of the background task. Since both tasks are in the same process the process must not terminate before both tasks terminate.
If you want the main task to wait for the completion of the background task before it terminates, the background task must be created in an inner block of the main task.
with Ada.Text_IO; use Ada.Text_IO;
procedure joined_task is
begin
-- create an inner block
declare
task background;
task body background is
begin
for I in 1 .. 10 loop
Put_Line ("Background:" & I'Image);
delay 1.5;
end loop;
end background;
begin
null;
end;
for I in 1 .. 10 loop
Put_Line ("Main:" & I'Image);
delay 1.0;
end loop;
end joined_task;
The output of this program is:
Background: 1
Background: 2
Background: 3
Background: 4
Background: 5
Background: 6
Background: 7
Background: 8
Background: 9
Background: 10
Main: 1
Main: 2
Main: 3
Main: 4
Main: 5
Main: 6
Main: 7
Main: 8
Main: 9
Main: 10
The main task waited for the background task to complete before executing its loop. In the first example both tasks executed in an overlapping manner, while the second example demonstrated how the main task waited for completion of its inner block before proceeding. The second example is functionally similar to using the join command in Java or in C or C++ thread libraries.
Upvotes: 2