Sharing variables across Apache threads with threads::shared

[prev] [thread] [next] [Date index for 2005/02/22]

From: Matthew Westcott
Subject: Sharing variables across Apache threads with threads::shared
Date: 15:02 on 22 Feb 2005
Hi,
I'm having trouble with the basics of sharing a variable across 
multiple Apache threads using threads::shared . The only code snippet 
I've been able to find on this subject is from the list archives from 
last month:
http://gossamer-threads.com/lists/modperl/modperl/77651#77651
I'm stuck at a much earlier stage than that, so I'm a bit lost in the 
details of that code - I think I'm working along the same sort of lines 
though.

My code is as follows:
---

package MyApache::MagicNumber;

use strict;
use warnings;

use threads;
use threads::shared;

use Apache::Const -compile => qw(OK);

use Apache::RequestRec ();
use Apache::RequestIO ();

my $magic_number : shared;

sub post_config {
     $magic_number = int(rand(100));
     my $now = gmtime(time);
     `echo "$now: MagicNumber initialised to $magic_number by PID $$" >> 
/tmp/magicnumber.log`;
     return Apache::OK;
}

sub handler {
     my $r = shift;
     $r->content_type('text/html');
     if ($r->args =~ /increment/) {
         $magic_number++;
         my $now = gmtime(time);
         `echo "$now: magic_number incremented to $magic_number by PID 
$$" >> /tmp/magicnumber.log`;
     }
     print "<html><body><p>the magic number 
is</p><h1>$magic_number</h1></body></html>";

     return Apache::OK;
}

1;

---
This is set up in httpd.conf like this:

PerlModule MyApache::MagicNumber
PerlPostConfigHandler MyApache::MagicNumber::post_config

<Location /magicnumber>
         SetHandler perl-script
         PerlResponseHandler MyApache::MagicNumber
</Location>

Repeatedly calling http://localhost/magicnumber?increment indicates 
that each thread is keeping its own copy of $magic_number - they are 
all starting from the same initial value set in post_config, at least. 
magicnumber.log contains the following:

Tue Feb 22 14:06:14 2005: MagicNumber initialised to 63 by PID 17452
Tue Feb 22 14:06:15 2005: MagicNumber initialised to 2 by PID 17454
Tue Feb 22 14:06:20 2005: magic_number incremented to 3 by PID 17459
Tue Feb 22 14:06:21 2005: magic_number incremented to 4 by PID 17459
Tue Feb 22 14:06:22 2005: magic_number incremented to 5 by PID 17459
Tue Feb 22 14:06:23 2005: magic_number incremented to 6 by PID 17459
Tue Feb 22 14:06:24 2005: magic_number incremented to 7 by PID 17459
Tue Feb 22 14:06:38 2005: magic_number incremented to 3 by PID 17462
Tue Feb 22 14:06:38 2005: magic_number incremented to 4 by PID 17462
Tue Feb 22 14:06:39 2005: magic_number incremented to 5 by PID 17462
Tue Feb 22 14:06:41 2005: magic_number incremented to 6 by PID 17462
Tue Feb 22 14:06:45 2005: magic_number incremented to 3 by PID 17458
Tue Feb 22 14:06:46 2005: magic_number incremented to 4 by PID 17458
Tue Feb 22 14:06:50 2005: magic_number incremented to 7 by PID 17462

I've checked that threads::shared is working correctly outside of 
mod_perl (via the examples on 
http://www.perl.com/pub/a/2002/06/11/threads.html ) - any suggestions 
where I'm going wrong?

I'm running mod_perl 1.99_12 on Apache 2.0.47 with Perl 5.8.0.

Thanks,
- Matthew

Sharing variables across Apache threads with threads::shared
Matthew Westcott 15:02 on 22 Feb 2005

Generated at 11:30 on 23 Feb 2005 by mariachi v0.52