The following module object, MonitorDemo, makes use of the Monitor module:
MonitorDemo: = LIB.MonitorSystem.System
M: = Monitor
print(S: ?String): entry
putItem(msg: ? String,V: ?integer,res: ?integer): entry
" = ".print
P1: = Process
fac(V: ?integer) -> res: ?integer:
%if V = 1 %then%
res := 1
res := V * fac(V - 1)
%for 1 %to% 5 %do%
P2: = Process
sum(V: ?integer) -> res: ?integer:
%for 1 %to% 10 %do%
res := res + V
%for 1 %to% 6 %do%
MonitorDemo is a constant object reference. It subpatterns from LIB.MonitorSystem.System (System for short below) and includes the String module.
It defines a singular Monitor object, M, which subpatterns from Monitor - defined in System. It has two local patterns Print and putItem that both subpatterns from entry - defined in System. The implementation of Monitor and entry in System guarantees that at most one process at a time executes print or putItem.
It defines two singular process objects P1, and P2, which both subpatterns from Process - defined in System.
P1 has a local pattern fac that computes the factorial of its argument. Execution of P1 begins with execution of the superpattern Process, which executes (%this Process).suspend - implying that P1 is temporarily suspended.When P1 is resumed by the scheduler in the MonitorSystem, it executes M.print("P1 resume\n") and then computes the 5 first factorial numbers and calls M.putItem.
P2 is similar except that it computes the function sum.
The for-statement %for first %to% last %do% S, is a pattern defining a control structure - and defined in the qBeta library.
Execution of MonitorDemo starts by execution M.init and then P1.start and P2.start.