No, that's not it. Your problem has nothing to do with thread.
Use «use strict; use warnings;»!!! $num_threads is undef when you execute the BEGIN block.
- The file is compiled.
- my $num_threads = 2; is compiled.
- my $thr = []; is compiled.
- BEGIN { ... } is compiled.
- ...
- for (my $ii=0; $ii<$num_threads; $ii++) { is compiled.
- ...
- BEGIN { ... } is executed.
- ...
- for (my $ii=0; $ii<$num_threads; $ii++) { is executed.
- ...
- print "done\n"; is compiled.
- The file is executed.
- my $num_threads = 2; is executed.
- my $thr = []; is executed.
- print "done\n"; is executed.