Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#================== xy_load.pl ============= #! /usr/bin/perl -w use 5.008; # 5.8 required for stable threading package xy_load; use strict; no strict 'subs'; no strict 'refs'; use warnings; use threads; use File::Basename; my $HOME = dirname($0); # directory the program loaded from push(@INC, $HOME); # Set in perl's include path list $| = 1; my $p = 0; # counter my $sub = "xy1"; # the name of program to load my $thr1 = threads->create(\&start_thread, $sub, $p ); $p++; my $thr2 = threads->create(\&start_thread, $sub, $p ); $thr1->join; $thr2->join; sub start_thread { my ($sub, $p )= @_; eval "require $sub"; # load the passed program &{"xy_load::" . $sub . "::Process"}($p); # and run it } #================ xy1.pm ============ package xy_load::xy1; ### I want $q as shared global to all invocations of "xy1", my $q : shared = 0; sub Process { my $p = $_[0]; my $tid = threads->self->tid; $| = 1; { lock ($q); $q++; print STDERR "TID: $tid, p = $p, q = $q\n"; } } 1; # success status for require
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Threads, dynamic loading programs, and using globals?
by pg (Canon) on Jan 04, 2003 at 21:20 UTC |