How can I freeze a task?
I mean, if I have a task
task body My_Task is
begin
accept Start;
loop
Put ("1");
Put ("2");
Put ("3");
...
Put ("n");
end loop;
end My_Task;
is there a way that I can “freeze” the task in its current state? If, for instance, the execution finished executing Put ("2");, how can I freeze it and later I can turn it to continue? I want to provoque a freeze from outside the task, and also from outside, order it to continue.
Update
I could sure implement, if I had the spec like
type State_Type is
(RUN,
FROZEN);
task type My_Task (State : State_Type) is
entry Start;
end My_Task;
the body
task body My_Task is
begin
accept Start;
loop
Put ("1");
Put ("2");
Put ("3");
...
Put ("n");
loop
if State = RUN then exit; end if;
end loop;
end loop;
end My_Task;
but it would not be the case because I had to wait for the nth Put instruction line (i.e., the task would not be actually frozen, because the inside loop would be running).
You can use a “selective accept” to allow your task to be interrupted by an external caller (another task or the main program). You can’t (easily) interrupt a task at an arbitrary point in its execution; instead, the task itself needs to determine when it will accept entry calls.
So you’ll probably want to replace your sequence
by a loop that calls
Putonce on each iteration, with aselectstatement that’s executed each time.Here’s a demo program that I just threw together. The counter increments once per second (approximately). The counter value is printed if
Runningis true; otherwise the loop proceeds silently. The main program alternatively calls Pause and Resume every 3 seconds.The
elseclause in theselectstatement cause it to continue executing if neither entry has been called.There may be a more elegant approach than this. It’s been a long time since I’ve really used Ada.