loops - 如何在Ada中多次调用一个任务

标签 loops task ada

我尝试调用一个任务 100 次,但它不起作用,看来我应该创建 100 个条目来做到这一点。那么您能给一些建议吗?

我现在正在学习如何使用 Ada 任务:-)

with Ada.Text_IO ; use Ada.Text_IO;
procedure Main is

   task type test1 is
      entry start;
   end test1;

   task body test1 is
   begin
      accept start;
      Put_Line("Haha");
   end test1;

   t1 : test1;
N : Integer := 10;
begin
   while N /= 0 loop
     t1.start;
     N := N - 1;
   end loop;
end Main;

E:\Ada Code\Simple\obj\main.exe 哈哈

引发了 TASKING_ERROR [2019-06-03 17:55:17] 进程退出,状态为1,运行时间:01.00s

最佳答案

您的问题是任务将在接受第一个集合点后运行完成。您可以通过在任务主体中添加循环来避免这种情况:

task body test1 is
begin
   loop
      accept start;
      Put_Line("Haha");
   end loop;
end test1;

(事实上,大多数任务都会有这样的循环)

该任务现在有一个无限循环,因此下一步是确保您的任务在主程序完成时终止。在这种情况下,您可以通过选择性接受来做到这一点:

task body test1 is
begin
   loop
      select
         accept start;
      or
         terminate;
      end select;

      Put_Line("Haha");
   end loop;
end test1;

关于loops - 如何在Ada中多次调用一个任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56425008/

相关文章:

c - 我在 C 程序中不断收到错误 "Time Limit Exceeded"。我应该如何提高我的代码效率?

python - 多个循环创建绘图的问题,出现错误

c# - 如何从 ReactiveCommand 捕获异常?

ada - 如何在字符中存储 ASCII 值(仅限 Ada 83)

lua - 如何从 Ada 代码控制 win32 中的 cmd 窗口?

image - Ada 中的默认整数'image() 宽度

python - 将数组对象更改为字符串... TypeError : list indices must be integers, not str

java - 使用循环从 JTextFields 获取值

c# - SQLite 异步任务和 Wait 运算符 - 任务会以正确的顺序执行吗?

c# - 在没有 ThrowIfCancellationRequested 的情况下停止任务