in reply to Threads, dynamic loading programs, and using globals?
package ex3.pm: use threads::shared; use strict; my $p; share($p);#a shared variable, and we will see that it has a per invoca +tion scope. sub process1 { while (1) { print "process1 thinks \$p = $p\n";#if it is shared, you will +see the bumps sleep(1); } } sub process2 { while (1) { $p ++; print "process2 bumped \$p to $p\n";#the bump should affect pr +ints from process1, if $p is shared. sleep(1); } } ex3.pl: demo the failure of share between invocations. use threads; use strict; my $th1 = threads->create (sub {require ex3;#invocation 1 process1(); }); my $th2 = threads->create (sub {require ex3;#invocation 2 process2(); }); $th1->join; ex4.pl: demo the succ of share within the same invocation. use threads; use ex3;#once for all use strict; my $th1 = threads->create (sub { process1(); }); my $th2 = threads->create (sub { process2(); }); $th1->join;
|
|---|