Ada 语句 'accept start do ' 中的任务未完成全部执行?

标签 task ada

Ada 'accept start do' 语句中的任务未完成其所有执行?

接受开始执行

这里的“do”关键字强制编译器完成 do 和 end 关键字之间的所有执行提及,但为什么在下面的代码中它没有发生?为什么它打印其他任务主体,即第一个(任务名称)而不是它自己的任务(即开始)在下面的代码中?请帮助我理解我哪里出错了?

procedure main is
   task first is
      entry start;
   end first;

   task body first is
   begin
      accept start ;   -- i am not using do here

      put_line("first");
      put_line("first");
      put_line("first");
   end first;

   task second is
      entry start;
   end second;

   task body second is
   begin
      accept start do -- here 'do' keyword will force compile to finish all the execution between keyword 'do' and end? but why it is mixing statement of task first? 
         for i in 1..10 loop
            put_line("second");
         end loop;
      end start;
   end second;

begin
   first.start;
   second.start;
end main;

   output-
   second
   second
   second
   second
   first   -- why first get printed between before completing second?
   first
   first
   second
   second
   second
   second
   second
   second

注意-每次它给出不同的o/p时,我期望如果第二个任务首先被激活,那么它应该首先完成do和end之间的所有语句,它不应该打印其他任务语句,直到第二个任务完成然后它应该去其他任务

最佳答案

接受 first.start 后,first 准备在获得处理器时输出一行,而 main 继续并调用第二个.start

接受 second.start 后,second 输出其 10 行,然后将控制权返回给 main

但是,如果在 Ada 运行时系统甚至操作系统的深处发现 put_line 涉及阻塞,会发生什么情况?

在这种情况下,second 被阻止,因此 first 可以自由地继续并输出其中一行。此时,first 很可能会再次被阻止,因此 second 有机会...

看来你的操作系统(和我的 - Mac OS X)在 put_line 内阻塞,或者至少可以阻塞。

总的来说,事实是标准输出 - 即您通过 putput_line 访问的内容 - 是您从两个任务访问的共享资源。 Ada 语言 ( ARM 9(8) ) 说

tasks can communicate indirectly by reading and updating (unprotected) shared variables, presuming the access is properly synchronized through some other kind of task interaction.

并且您没有进行任何形式的同步,因此系统可以做它想做的事;结果不是由语言定义的。在类似的情况下,我看到一个任务输出“hello”,另一个任务输出“world”,结果是“hweolrrlod”。

关于Ada 语句 'accept start do ' 中的任务未完成全部执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28748433/

相关文章:

c# - 如果发生任何异常,如何使 Task.WaitAll() 中断?

character-encoding - 这个 Ada 字 rune 字格式是在哪里定义的?在 Ada 2005 中有什么变化?

sockets - 流读取阻止UDP GNAT

Ada Get_Line 不等待获取响应

ada - 带注释的 Ada 语言(Anna)

c# - for 循环问题中的 System.Threading.Tasks

typescript - VSCODE 任务 typescript TS5042 内部版本 : Option 'project' cannot be mixed with source files on a command line

Ansible 默默地跳过任务/调试

C# 读写 TextFile 在中间结束

function - ADA - 前置条件和后置条件不起作用?