17.3. $LOCK classes

All synchronization objects subtype from $LOCK. In addition to primitive $LOCK classes, some synchronization classes return $LOCK objects to allow different kinds of locking. The concrete type of the returned object is dependent on the pSather implementation.

17.3.1. Locking example

Example 17-3. This code implements five dining philosophers. The philosophers are seated at a round table and forced to share a single chopstick with each neighbor. They alternate between eating and thinking, but eating requires both chopsticks.

chopsticks ::= #ARRAY{MUTEX}(5);
loop chopsticks.set!(#MUTEX) end;
parloop
   i::= 0.upto!(4);
do
   loop
      think;
      lock
      when chopsticks[i], chopsticks[(i+1).mod(5)] then
         eat
      end;
   end;
end;