comp.lang.ada
 help / color / mirror / Atom feed
From: melody@arabian.gatech.edu (Melody Eidbo)
Subject: Shared variable mysteries solved
Date: 29 Jun 89 20:15:15 GMT	[thread overview]
Message-ID: <18940@gatech.edu> (raw)

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

                 reply	other threads:[~1989-06-29 20:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox