From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.7 required=5.0 tests=BAYES_00,INVALID_DATE, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!gatech!arabian.gatech.edu!melody From: melody@arabian.gatech.edu (Melody Eidbo) Newsgroups: comp.lang.ada Subject: Shared variable mysteries solved Message-ID: <18940@gatech.edu> Date: 29 Jun 89 20:15:15 GMT Sender: news@gatech.edu Reply-To: melody@arabian.gatech.edu (Melody Eidbo) Organization: Software Engineering Research Center, Georgia Tech. List-Id: 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