multithreading - Array getting destroyed when thread exits in perl -
i trying use threading parsing 2 different types of files. subroutines share no data @ all.
# parse header files $hdr_thrd = threads -> create(\&parser::parse_header_file, $path); # parse input template files $tmplt_thrd = threads -> create(\&templateparser::parse_template); # join threads $tmplt_thrd -> join(); $hdr_thrd -> join(); # uses data obtained above 2 threads &parser::parse_xml_template();
the problem comes when parse_xml_template
function tries access array @templateparser::array
. array has no data @ point getting filled inside parse_template
function. missing something?
you're trying share data across threads without sharing it. need use :shared
or share()
on variable.
you wouldn't have problem @ if avoid global vars should.
sub parse_template { @tmplt_result; ... return \@tmplt_result; } $hdr_thrd = threads->create(\&parser::parse_header_file, $path); $tmplt_thrd = threads->create(\&templateparser::parse_template); $tmplt_result = $tmplt_thrd->join(); $hdr_result = $hdr_thrd->join();
(explicit sharing not necessary when returning value via join
.)
of course, needlessly creates 2 threads (for total of three) when 2 suffice. instead, use:
sub parse_template { @tmplt_result; ... return \@tmplt_result; } $hdr_thrd = threads->create(\&parser::parse_header_file, $path); $tmplt_result = templateparser::parse_template(); $hdr_result = $hdr_thrd->join();
Comments
Post a Comment