Tutorial: Concurrency


 

Concurrency is a large and interesting topic, so this will only scratch the surface.

How does it look?

Concurrency is based on components, as introduced in the section about co-routines, but we need to use one of the built-in attributes of component in order to start a thread concurrently with respect to the rest of the program execution:

fork

It looks like e.g. aCmp.fork, where aCmp must be some specialization of the component basic pattern. Similarly, to kill a running thread which is associated with a particular component, use:

kill

like in aCmp.kill. There ought to have been a similar command suspend, but since (for historical reasons) there is a special imperative SUSPEND, the grammar does not allow this. Instead, temporarily, the following command has been defined on components:

_suspend (* NB: partially implemented *)

The effect of aCmp._suspend would be to suspend that component, as if a SUSPEND imperative had been executed in that thread. Please note that the current implementation of aCmp._suspend only supports suspending a component during its own execution, i.e. you cannot "suspend somebody else." Finally, to handle run-time errors and manage threads in general, the following command has been defined but not implemented:

status (* NB: not implemented *)

The purpose of evaluating aCmp.status is to detect whether aCmp is running, suspended, or terminated, and in case is has terminated, it should somehow deliver information about the termination status of the component. This could be "Normal," "Divide by zero," "NONE-Reference," or whatever might stop a thread.

Example 9

This example is probably best executed in running mode (don't give the -i option first), it gets too boring otherwise!


(* FILE ex9.gb *)
-- betaenv:descriptor --
(#
   done: @integer
     (# do value+1->value; (if value=3 then '\n'->stdio if)#);
   cmp1: @|
     (# do (for 50 repeat '.'->stdio for); done #);
   cmp2: @|
     (# do (for 100 repeat 'x'->stdio for); done #)
do
   cmp1.fork;
   cmp2.fork;
   (for i:75 repeat (if i mod 7 = 0 then '\nmain '->stdio if)for);
   done
#)

This actually finishes the main topics. The only things left now for the next and following sections are various odds and ends which are nevertheless needed.

 


Signed by: eernst@cs.auc.dk. Last Modified: 3-Jul-01