Perl6 Reading Interference

I need to have several threads, each of which is read from the same socket or from $ * IN; however, there seems to be a mistake because everyone is trying to read from the same source (I think). What is the best way to solve this problem? Thank!

my $x = start { prompt("I am X: Enter x: "); }
my $y = start { prompt("I am Y: Enter y: "); }

await $x, $y;
say $x;
say $y;

And here are the errors:

I am X: Enter x: I am Y: Enter y: Tried to get the result of a broken Promise
  in block <unit> at zz.pl line 4

Original exception:
    Tried to read() from an IO handle outside its originating thread
      in block  at zz.pl line 1

Thank!

+4
source share
1 answer

In the last Rakudo development snapshot, your code really works without any exceptions on my system ...
However, it still asks for both ( I am X: Enter x: I am Y: Enter y:) values ​​at once .

So that the second promptwaits for the completion of the first, you can use Lock:

#--- Main code ---

my $x = start { synchronized-prompt "I am X: Enter x: "; }
my $y = start { synchronized-prompt "I am Y: Enter y: "; }

await $x, $y;
say $x.result;
say $y.result;


#--- Helper routines ---

BEGIN my $prompt-lock = Lock.new;

sub synchronized-prompt ($message) {
    $prompt-lock.protect: { prompt $message; }
}

, , . Lock.new synchronized-prompt, . , , BEGIN phaser, .

+4

Source: https://habr.com/ru/post/1684981/


All Articles