comp.lang.ada
 help / color / mirror / Atom feed
* Shared variable mysteries solved
@ 1989-06-29 20:15 Melody Eidbo
  0 siblings, 0 replies; only message in thread
From: Melody Eidbo @ 1989-06-29 20:15 UTC (permalink / 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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1989-06-29 20:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1989-06-29 20:15 Shared variable mysteries solved Melody Eidbo

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