Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 需要在perl中将数据推送到线程_Multithreading_Perl - Fatal编程技术网

Multithreading 需要在perl中将数据推送到线程

Multithreading 需要在perl中将数据推送到线程,multithreading,perl,Multithreading,Perl,我正在尝试创建一个多线程程序,其中线程执行作业,数据使用共享变量推送 如何将数据从主线程推送到子线程 这里的示例脚本给出了一个错误“thread_测试行10上的共享标量值无效” 您必须先将$thread_data{1}初始化为共享数组引用,然后再将其推入其中 $thread_data{1} = shared_clone([]); 不要那样做。改用Thread::Queue use Thread::Queue; my $work_q = Thread::Queue -> new();

我正在尝试创建一个多线程程序,其中线程执行作业,数据使用共享变量推送

如何将数据从主线程推送到子线程

这里的示例脚本给出了一个错误“thread_测试行10上的共享标量值无效”


您必须先将
$thread_data{1}
初始化为共享数组引用,然后再将其推入其中

$thread_data{1} = shared_clone([]);

不要那样做。改用
Thread::Queue

 use Thread::Queue;

 my $work_q = Thread::Queue -> new();

 my $thr = threads -> create ( \&thread_that_does_stuff );

 $work_q -> enqueue ( \@anon ); 
 $work_q -> end();

 $thr -> join(); 

 sub thread_that_does_stuff {
   while ( my $thing = $work_q -> dequeue ) {
   print join ( ":", @$thing );
   }
 }
Thread::Queue
是一个FIFO队列,它允许您在线程之间传递数据,而不用担心(太多!)锁定语义

 use Thread::Queue;

 my $work_q = Thread::Queue -> new();

 my $thr = threads -> create ( \&thread_that_does_stuff );

 $work_q -> enqueue ( \@anon ); 
 $work_q -> end();

 $thr -> join(); 

 sub thread_that_does_stuff {
   while ( my $thing = $work_q -> dequeue ) {
   print join ( ":", @$thing );
   }
 }