[comp.lang.ada] Shared variable mysteries solved

melody@arabian.gatech.edu (Melody Eidbo) (06/30/89)

In <8610@june.cs.washington.edu>, Nicholas Tan Chee Hiang writes:

> I am trying to understand concurrency in Ada.  Can someone please tell
> me why the variable Unprotected below is never updated?  Thanks!

> (Main program and task that writes to shared variable deleted for brevity)
>  task body Access_Variable is
>    Letter : Character := Unprotected;
>  begin
>    loop
>      Put (Letter);
>      delay 3.0;
>    end loop;
>  end Access_Variable;

  The problem is that you have assigned the variable "Letter" in the 
declarative part of the task.  This assignment will take place when the
task's declarative part is elaborated, NOT when the task actually starts
executing.  Remember that a task created by an object declaration begins
execution only after the enclosing declarative part is elaborated. 

  The order of events is:

  elaboration of the main program declarative part
  elaboration of the tasks' declarative parts
  (concurrently) main program and tasks begin execution

  Therefore, your assignment statement to "Letter" takes place before the
other task executes, and the "Unprotected" shared variable does not have
the correct value. 

(BTW, there is an excellent discussion of the Ada task model in Norm
 Cohen's book, "Ada as a Second Language".)


                               Melody Moore Eidbo