Multithreading 需要在perl中将数据推送到线程
我正在尝试创建一个多线程程序,其中线程执行作业,数据使用共享变量推送 如何将数据从主线程推送到子线程 这里的示例脚本给出了一个错误“thread_测试行10上的共享标量值无效”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_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 );
}
}