Documentation for method condition assembled from the following pages:

Class: X::Phaser::PrePost §

From X::Phaser::PrePost

(X::Phaser::PrePost) method condition §

method condition(--> Str:D)

Returns the part of the source code that describes the phaser condition.

Class: Lock §

From Lock

(Lock) method condition §

Defined as:

method condition(Lock:D: )

Returns a condition variable as a Lock::ConditionVariable object. Check this article or the Wikipedia for background on condition variables and how they relate to locks and mutexes.

my $l = Lock.new;
$l.condition;

You should use a condition over a lock when you want an interaction with it that is a bit more complex than simply acquiring or releasing the lock.

constant ITEMS = 100;
my $lock = Lock.new;
my $cond = $lock.condition;
my $todo = 0;
my $done = 0;
my @in = 1..ITEMS;
my @out = 0 xx ITEMS;
 
loop ( my $i = 0$i < @in$i++ ) {
    my $in := @in[$i];
    my $out := @out[$i];
    Thread.start{
      my $partial = $in² +1;
      if $partial.is-prime {
          $out = $partial but "Prime";
      } else {
          $out = $partial;
      }
      $lock.protect{
         $done++;
         $cond.signal if $done == $todo;
      } );
    } );
    $todo++;
}
$lock.protect{
    $cond.wait({  $done == $todo } );
});
 
say @out.map: { $_.^roles > 2 ?? $_.Num ~ "*" !! $_ };
# OUTPUT: «2* 5* 10 17* 26 37* 50 65 82 101* … » 

In this case, we use the condition variable $cond to wait until all numbers have been generated and checked and also to .signal to another thread to wake up when the particular thread is done.