Hi,
I have a working model where a main program starts threads
that dynamically load another named program.
It is like a plug-in style system.
I run it under Windows 2000, Activestate 5.8.
Now, I want multiple instances of the same loaded program
to share common resources. In my example I want a global static $q to be
accessed by the second invocation of "xy1".

1) How shall I do it if I place this resource in the main file?
2) How do I place this resource just in the loaded file (preferred)?
If this model can be inproved, please advise.

You can test it; In the stripped-down version below,
cut the code into 2 files, "xy_load.pl" and "xy1.pm",
and place in same directory.
TIA
J. Merc
#================== 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

In reply to Threads, dynamic loading programs, and using globals? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.