# Producer/consumer parallel/concurrent processes. Derived loosely from the Algol 68 Revised Report, section 11.12. Reads integers from standard input until one is negative, and writes them one to a line on standard output via a buffer. Assumes that concurrent access to *different* elements of "buffer" is not a problem. If it is, then we need, eg, SEMA bufprotect = LEVEL 1; and DOWN bufprotect; ... buffer[...] ...; UP bufprotect; round each use of "buffer". #
BEGIN
INT bufsize = 20;
[bufsize] INT buffer;
INT index := 0,
exdex := 0;
SEMA full = LEVEL 0,
empty = LEVEL bufsize;
PAR BEGIN
# produce #
WHILE INT n;
read (n);
DOWN empty;
buffer [index %*:= bufsize +:= 1] := n; # %* is "mod" operator #
UP full;
n >= 0
DO SKIP OD,
# consume #
WHILE
DOWN full;
INT n = buffer [exdex %*:= bufsize +:= 1];
n >= 0
DO print ((n, newline));
UP empty
OD
END
END