7.3.1 Library Style

We will frequently use cells to implement abstract data structures. In this case, we can use the Oz library functions directely.

You can create a new cell by calling the function Cell.New whose argument specifies the initial content of your cell. You can always look up the actual content of a cell applying the function Access. The content can be changed by using the procedure Assign.

<Test Cell Library>=
declare C = {Cell.new unit}
{Inspect {Access C}}
{Assign C hi}
{Inspect old#{Access C}}
{Inspect old#{Exchange C $ _}}
{Inspect new#{Access C}}
{Access C} = 'the new value of C was bound late' 

There is an procedure Exchange which combines Access with Assign into an atomic operation. It is called in the form:

{Exchange C ?Old ?New}

The Old value is access before the new one is assigned. It is important that Exchange is atomic in the presence of concurrent processes: this means than the exchange operation cannot be interrupted by any other concurrent process. Otherwise, the old value might get corrupted while the new is computed. Such an accident could easily turn the state of your whole program inconsistent.

But how can one simultaneouly access the old value and assign the new value if the new value is to be computed in dependency of the old value? The trick in Oz is that one can always describe the new value by a free logic variable whose value can then be computed later on from the old value.


Denys Duchier, Claire Gardent and Joachim Niehren
Version 1.3.99 (20050412)