Condition variables are used in Lock
s to wait for a particular condition to become true. You will normally not create one from scratch, but call Lock.condition
to acquire one on a particular Lock
.
Methods §
method wait §
Defined as:
multi method wait( --> Nil )multi method wait( --> Nil )
Without any predicate, it waits on the condition variable itself; with a predicate, waits until the code returns a truish value.
my = 100; my ; my ; for ^
The condition we obtain from the $l
lock is awaited using a predicate, in this case, check if the counter is still zero. When it takes another value, the program flow continues in the next instruction.
method signal §
Defined as:
method signal()
If and only if there are any threads that have previously waited on the condition variable, it unblocks at least one of them. Let's see how it works in this example:
constant ITEMS = 100;for 1..15 ->
We are repeating 15 times the same operation: start 100 threads, every one of which modify a single element in an array. We protect
the modification of a global variable, $done
, and use signal
to wake a up another thread to do its thing. This outputs the first elements of the generated arrays.