Re: Sharing variable in threaded mpm not working as expected

Pawel Herman <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <CAGYVaJxv3Tt++1JhyvKgWAKsncqP_rFMDzvvo8YbTVRQk6E-=Q@mail.gmail.com>
Hello Torsten,

I tried your suggestions push(@fruits,share('apple')); and
push(@fruits,share(do {my $x='apple'})); but both did not work because
share can only take references, arrays, or hashes as input.  I tried
similar variations but no luck.  The fruits array is still initialized
multiple times.  I even created the global counter like you suggested:

my $counter :shared = 0;

and then in the handler method:

      {
               logTestMessage('Waiting for lock.');
               lock($counter);
               logTestMessage('Acquired lock.');
               if($counter == 0) {
                       logTestMessage('Increasing counter.');
                       $counter++;
               }
               logTestMessage('Releasing lock.');
       }

but even then, the counter is increased multiple times.  As far as
populating the fruits array goes, I believe after reading
http://perldoc.perl.org/threads/shared.html that my approach is
correct.  It specifically says in the Bugs and Limitations section
that one first declares a variable shared and then populates it.
Since 'apple', 'banana', and 'peach' are scalars, they do not have to
be explicitly shared.

You pointed me to a module in the modperl test suite.  I downloaded
modperl from http://perl.apache.org/download/index.html but there is
no t/response/TestPerl/ithreads.pm.  I also searched for the string
'threads::shared' in all directories but no luck.  Where did you get
this pm?

Sincerely,
Pawel

2011/8/2 Torsten Förtsch <[email protected]>:
> On Tuesday, 02 August 2011 20:24:46 Fred Moyer wrote:
>> I haven't used the worker mpm, but it looks like you might want to
>> preload your handler in the httpd parent process.
>
> Unlikely that that will help. threads::shared works by creating a
> separate perl interpreter to keep the shared values. That interpreter
> does not execute any program. It simply serves as shared storage. When a
> variable is then marked as shared it is assigned magic to have the normal
> accessor methods use the storage in the other interpreter. So, no matter
> when the variable or the interpreter executing a program is created
> shared variables are always fetched from the special storage interpreter.
>
> The special storage interpreter is a global variable at C-level.
>
> t/response/TestPerl/ithreads.pm in the modperl test suite exercises
> shared variables. I'd take this as a starting point. First, I'd create a
> simple shared scalar as a global counter. If that works (which I think it
> does) the rest is how to use threads::shared correctly.
>
> As for your code I think you could try to push shared apples like this
>
>  push(@fruits,share('apple'));
>
> Perhaps even
>
>  push(@fruits,share(do {my $x='apple'}));
>
> Torsten Förtsch
>
> --
> Need professional modperl support? Hire me! (http://foertsch.name)
>
> Like fantasy? http://kabatinte.net
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.