Looking for Monitor example, addendum
Boris Gaertner
Boris.Gaertner at gmx.net
Sun Mar 19 14:02:21 UTC 2006
Hello again, Chris,
your example is not that bad; your only mistake is that you signal
a foreign monitor without entering its critical region.
You have to replace goalMonitor signal.
with: goalMonitor critical: [goalMonitor signal].
and in the same way
fives signal. with: fives critical: [fives signal].
nonFives signal. with: nonFives critical: [nonFives signal].
The following works:
| goal work fives nonFives counter goalReached goalMonitor |
goal _ (1 to: 1000) asOrderedCollection.
work _ OrderedCollection new.
fives _ Monitor new.
nonFives _ Monitor new.
counter _ 0.
goalReached _ false.
goalMonitor _ Monitor new.
[ fives critical:
[ [ fives waitUntil: [ counter \\ 5 = 0 ].
goalReached or:
[ work add: (counter _ counter+1).
nonFives critical: [nonFives signal].
goalReached _ counter >= goal size ] ] whileFalse.
goalMonitor critical: [goalMonitor signal] ] ] fork.
[ nonFives critical:
[ [ nonFives waitWhile: [ counter \\ 5 = 0 ].
goalReached or:
[ work add: (counter _ counter+1).
fives critical: [fives signal].
goalReached _ counter >= goal size ] ] whileFalse.
goalMonitor critical: [goalMonitor signal] ] ] fork.
goalMonitor critical:
[ goalMonitor waitUntil: [ goalReached ].
].
work = goal
-----------------------
Note that the process 'fives' adds the seqeuence 1, 6, 11 into your
collection. To fix that, both processes should use the wait condition
[ counter \\5 = 4].
Alternatively, the counter could start at 1 and be incremented after
insertion into the collection. The correct expression for goalReached
would then be: counter > goal size.
Greetings, Boris
More information about the Squeak-dev
mailing list
|