perl6线程读取干扰

时间:2017-09-03 15:05:51

标签: multithreading perl6

我需要从同一个套接字或从中读取多个线程 $ * IN;然而,似乎有错误,因为每个人都试图从相同的来源读取(我认为)。解决此问题的最佳方法是什么?谢谢!!

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;

以下是错误:

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

谢谢!

1 个答案:

答案 0 :(得分:4)

在Rakudo的最新开发快照中,您的代码实际上可以正常工作而不会在我的系统上抛出任何异常...
但是,它仍会立即询问两个值(I am X: Enter x: I am Y: Enter y:)。

要使第二个prompt等到第一个#--- 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

Lock.new

棘手的部分是,在线程开始并发使用之前,需要初始化锁。这就是我在程序主线中调用synchronized-prompt子程序的add_filter( 'woocommerce_continue_shopping_redirect', 'my_changed_woocommerce_continue_shopping_redirect', 10, 1 ); function my_changed_woocommerce_continue_shopping_redirect( $return_to ){ $return_to = wc_get_page_permalink( 'checkout' ); return $return_to; } add_filter( 'wc_add_to_cart_message_html', 'my_changed_wc_add_to_cart_message_html', 10, 2 ); function my_changed_wc_add_to_cart_message_html($message, $products){ if (strpos($message, 'Continue shopping') !== false) { $message = str_replace("Continue shopping", "Go the checkout", $message); } return $message; } 之外的.grid { width: 100%; max-width: 900px; margin: 0 auto; background: #fff; } .grid::after { content: ""; display: block; clear: both; } .grid-item { width: 21.833%; padding-bottom: 21.833%; overflow: hidden; float: left; background: #BBB; transform: rotate(45deg); margin: 5.5%; margin-top: -11%; } .grid-item:nth-child(1), .grid-item:nth-child(2), .grid-item:nth-child(3) { margin-top: 5%; } .grid-item:nth-child(5n+4) { margin-left: 21.9%; } .grid-item:nth-child(5n+6) { clear: left; } .grid-item:nth-child(5n+6):last-of-type { margin-left: 38.25%; } .grid-item:hover { background: #000; } .grid-inner { box-sizing: border-box; position: absolute; top: 0; left: 0; width: 100%; height: 100%; transform: rotate(-45deg); text-align: center; padding-top: 40%; font-size: 1em; } .grid-inner:hover a span { display: none; } .grid-inner:hover:after { box-sizing: border-box; position: absolute; top: 0; left: 0; width: 100%; height: 100%; transform: rotate(-45deg); text-align: center; padding-top: 40%; font-size: 1em; background-color: black; } .grid-inner-hover { opacity: 0; } .grid-item:hover .grid-inner-hover { opacity: 1; color: #fff; } 的原因。我没有在程序顶部执行此操作,而是使用BEGIN phaser,以便将其放在子例程旁边。