wait
ErrorsCollection

wait

Synthesised documentation from type/Supply type/Lock/ConditionVariable

From type/Supply

See Original text in context

method wait(Supply:D:)

Taps the Supply it is called on, and blocks execution until the either the supply is done (in which case it evaluates to the final value that was emitted on the Supply, or Nil if not value was emitted) or quit (in which case it will throw the exception that was passed to quit).

my $s = Supplier.new;
start {
  sleep 1;
  say "One second: running.";
  sleep 1;
  $s.emit(42);
  $s.done;
}
$s.Supply.wait;
say "Two seconds: done";

From type/Lock/ConditionVariable

See Original text in context

multi method wait--> Nil )
multi method wait&predicate --> Nil )

Without any predicate, it waits on the condition variable itself; with a predicate, waits until the code returns a truish value.

    my $times = 100;
    my $tried;
    my $failed;
    for ^$times {
        my $l = Lock.new;
        my $c = $l.condition;
        my $now1;
        my $now2;
        my $counter = 0;
        my $t1 = Thread.start({
            $l.protect({
                $c.wait{ $counter != 0 } );
                $now1 = now;
            });
        });
 
        my $t2 = Thread.start({
            $l.protect({
                $counter++;
                $c.signal();
            });
        });
 
        $t1.join();
        $now2 = now;
        $t2.join();
 
        $tried++;
        last if $failed = ( !$now1.defined or $now1 > $now2 );
    }

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.