并发Ada程序中子类型使用问题

问题描述 投票:0回答:1

我正在研究 Ada 中的并发编程问题。任务是模拟一座桥梁,不同的汽车可以从不同的方向通过。复杂之处在于,如果有救护车,汽车必须给予它绝对优先权。

这是我开发的代码:

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   type Direccion is (Norte, Sur);

   task type Vehiculo (ID : Integer; Acceso : Direccion);
   task type Ambulancia;

   protected type Puente is
      entry Entrar (ID : Integer; Acceso : Direccion);
      entry Salir (ID : Integer);
   private
      Puente_Ocupado : Boolean := False;
      Ambulancia_Esperando : Boolean := False;
      Ambulancia_En_Puente : Boolean := False;
      Coches_En_Espera : Integer := 0;
      Ambulancia_En_Espera : Boolean := False;

      procedure Entrar_Ambulancia;
      procedure Entrar_Coche(ID : Integer; Acceso : Direccion);
   end Puente;

   task body Vehiculo is
   begin
      Put_Line ("El coche" & Integer'Image(ID) & " está en ruta en dirección " & Direccion'Image(Acceso) & "!");

      Puente.Entrar(ID, Acceso);
      delay(1.0); -- Simula el tiempo que tarda en cruzar el puente
      Puente.Salir(ID);
   end Vehiculo;

   task body Ambulancia is
   begin
      Put_Line ("La ambulancia 112 está en ruta");
      Puente.Entrar(112, Norte); -- Cambiado a un acceso específico, puedes ajustar según tus necesidades
      delay(0.5); -- Simula el tiempo que tarda en cruzar el puente
      Puente.Salir(112);
   end Ambulancia;

   protected body Puente is
      entry Entrar (ID : Integer; Acceso : Direccion) when not Puente_Ocupado is
      begin
         if ID = 112 then
            Entrar_Ambulancia;
         else
            Entrar_Coche(ID, Acceso);
         end if;
      end Entrar;

      entry Salir (ID : Integer) when Puente_Ocupado is
      begin
         if ID = 112 then
            Ambulancia_En_Puente := False;
         else
            Puente_Ocupado := False;
            Coches_En_Espera := 0;
         end if;
      end Salir;

      procedure Entrar_Ambulancia is
      begin
         if Puente_Ocupado then
            Ambulancia_Esperando := True;
            Ambulancia_En_Espera := True;
            Put_Line ("+++++Ambulancia 112 espera para entrar");
         else
            Puente_Ocupado := True;
            Ambulancia_En_Puente := True;
            Put_Line ("+++++Ambulancia 112 está en el puente");
         end if;
      end Entrar_Ambulancia;

      procedure Entrar_Coche(ID : Integer; Acceso : Direccion) is
      begin
         if Puente_Ocupado or Ambulancia_En_Puente then
            Coches_En_Espera := Coches_En_Espera + 1;
            Put_Line ("El coche" & Integer'Image(ID) & " espera a la entrada " & Direccion'Image(Acceso) & ". Esperan " & Integer'Image(Coches_En_Espera) & " coches.");
         else
            Puente_Ocupado := True;
            Put_Line ("El coche" & Integer'Image(ID) & " entra en el puente. Esperan en la " & Direccion'Image(Acceso) & ": " & Integer'Image(Coches_En_Espera) & " coches.");
         end if;
      end Entrar_Coche;
   end Puente;
   
   -- Creación de instancias
   Coche1 : Vehiculo(1, Norte);
   Coche2 : Vehiculo(2, Sur);
   Coche3 : Vehiculo(3, Norte);
   Coche4 : Vehiculo(4, Sur);
   Coche5 : Vehiculo(5, Norte);
   Amb : Ambulancia;
   
begin   
   -- Esperar la finalización de los procesos
   while not (Coche1'Terminated and Coche2'Terminated and Coche3'Terminated and Coche4'Terminated and Coche5'Terminated and Amb'Terminated) loop
      null;
   end loop;

   -- Mensajes finales
      Put_Line("Todos los vehículos han cruzado el puente y la ambulancia ha completado su ruta.");
end Main;

当前代码生成错误,指示“表达式或调用中子类型标记的使用无效”。我使用子类型来表示方向(北,南),“(北,南)”,并且在尝试在某些表达式或调用中使用它们时似乎存在问题。

我将不胜感激任何有关如何解决此问题的指导以及有关如何提高代码效率或清晰度的任何建议。

编译器错误:

main.adb:27:7: error: invalid use of subtype mark in expression or call
main.adb:29:7: error: invalid use of subtype mark in expression or call
main.adb:35:7: error: invalid use of subtype mark in expression or call
main.adb:37:7: error: invalid use of subtype mark in expression or call

  • 我尝试查看 Ada 文档,但无法确定代码中的具体问题。
  • 我使用子类型来表示地址,我怀疑该错误可能与它们在任务内的过程调用中的使用有关。 预先感谢您提供的任何帮助。
concurrency task ada subtype
1个回答
0
投票

您的问题是在任务正文中使用

Puente

定义

protected type Puente is
创建了一个名为
Puente
的类型。
也许您打算创建一个受保护的对象,就像这样
protected Puente is

在类型定义内部(如

Puente
),类型的名称指的是类型的当前实例(对象),这就是在
Puente

的主体中不会出现类似错误的原因
© www.soinside.com 2019 - 2024. All rights reserved.