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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: ffc1e,fac1372a6e25492a,start X-Google-Attributes: gidffc1e,public X-Google-Thread: 103376,fac1372a6e25492a,start X-Google-Attributes: gid103376,public From: "James S. Rogers" Subject: Ada Protected Object Turorial #2: Overview of Tasks Date: 1999/12/18 Message-ID: <83hu2h$bba$1@bgtnsc01.worldnet.att.net>#1/1 X-Deja-AN: 562551089 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc01.worldnet.att.net 945585041 11626 12.74.130.99 (19 Dec 1999 06:30:41 GMT) Organization: AT&T WorldNet Services NNTP-Posting-Date: 19 Dec 1999 06:30:41 GMT Newsgroups: comp.programming.threads,comp.lang.ada Date: 1999-12-19T06:30:41+00:00 List-Id: Overview of Ada Tasks. Tasks are entities whose execution may proceed in parallel. A task is an object of a task type. As with other types in Ada, the task type may be explicitly named by the programmer, or it may be an anonymous type. Task objects are created in the same ways as other objects: they may be declared by an object declaration, or created dynamically using an allocator. Tasks may be nested within other program units, in the same manner as subprograms (procedures and functions) and packages. Common task activities: Task Creation: A task is created when it is declared or dynamically allocated. The creator can communicate with the task during creation by initializing discriminants to the task. Task Activation: After creation the creator waits for the task to activate. No explicit action is required by the creator. The task will automatically activate as a result of its creation. Task Termination: Every task has a master. The master cannot terminate until all the tasks it created terminates. The created tasks are called children of the master. A task will terminate when it completes all its activities. It can also be terminated by the master through an abort command. Rendezvous: A rendezvous is a method for tasks to communicate directly with one another. The rendezvous synchronizes tasks at the point of communication. A calling task calls an entry in another task. The called task accepts the entry call. If the calling task calls the entry before the called task is ready to accept it, the calling task will wait for the called task. If the called task reaches the accept point before the calling task calls the entry, the called task will wait for a calling task. Entry parameters allow data to be passed between tasks during a rendezvous. Protected Objects: Protected objects provide mutual exclusion for data in shared memory between tasks. They allow tasks to communicate either synchronously or asynchronously. Tasks communicate indirectly by reading and writing components of the protected objects. Unprotected Shared Variables: Ada does allow you to use unprotected shared variables so that you can implement user-defined low level synchronization between tasks. It is the responsibility of the developer to ensure the integrity of the data. In addition to the basic tools described above, Ada provides finer control of the interactions between tasks through the many uses of the select clause. The select clause can be used to allow a task to accept several different entry calls each time through a loop, much like processing a case statement. The select clause can have a delay alternative, allowing a calling task to limit the amount of time it waits for the called task to accept the entry, or for a protected object to open the block on an entry. The select clause can have an abort option, allowing two activities to proceed simultaneously. When one of the options finishes the other option is automatically aborted. This is called Asynchronous Transfer of Control. One of the options must be an entry call or a delay. The other option is defined as the abortable part of the statement. An example lifted from the Ada 95 Rationale concerns having a database operation cancelled by typing a special key on the input device. declare Database_Txn : Transaction; -- declare a transaction, commit or abort during finalization begin select -- wait for a cancel key from the input device Input_Device.Wait_For_Cancel; -- the Satus remains Incomplets, so that the transaction will not commit then abort -- do the transaction Read(Database_Txn, ...); Write(Database_Txn, ...); ... Set_Status(Database_Txn, Succeeded); -- set status to ensure the transaction is committed end select; -- Finialize of Database_Txn will be called here and, -- based on the recorded status, will either commit or -- abort the transaction. end; I have left out the details of how the transaction type is defined so that Finalization handles the roll-back or commit of the database. In C++ terms, this would be handled by the destructor for Database_Txn. Ada clearly has more to offer concurrent programming than the ability to create threads and create mutexes. Most of your concurrent programming needs can be handled by the tools Ada provides, without forcing the programmer into more risky low level programming. At the same time, Ada allows the programmer to perform all the risky low level programming required to do the job. Jim Rogers Colorado Springs, Colorado