Jerry,

Jerry D. Hedden schreef:
> It would be very helpful if you would post a "complete" example that
> shows the problem you're having.

OK. In short a small test-program: (I left out the standard "REAPER"-code).


#! /usr/bin/perl

use threads;
use threads::shared;

my $a : shared = 1;

$SIG{'CHLD'} = \&REAPER;

$thr = threads->create(\&thr1);
$thr->detach;

$a=1;

print "main program start: a = $a \n";
sleep 4;
print "main program end: a = $a \n";
exit;

################### end main program ##########

sub thr1 {
sleep 1;
print "thread start: a = $a \n";
sleep 2;
$a=2;
print "thread end: a = $a \n";
return;
}; # end sub


So,
- the program starts and starts a sub-thread.
- it sets a to 1.
- the main program print "a = 1"
- one second later, the thread also prints this value
- 2 seconds later, the thread changes that value to 2, prints it and
returns.
- one second later, the main program print "a" again, which is not 2.

[EMAIL PROTECTED]:~/devel/perl/threads> ./thr1.pl
main program start: a = 1
thread start: a = 1
thread end: a = 2
main program end: a = 2



Now, what I do is change the code so that the main program is in one
file "thr1.pl" and the thread-code is in a second file "thr1_sub.pm".
This file is "included" into the main program using a "require" line.


File "thr1_.pl":

#! /usr/bin/perl
use threads;
use threads::shared;

my $a : shared = 1;

require "./thr1_sub.pm";

$SIG{'CHLD'} = \&REAPER;

$thr = threads->create(\&thr1);
$thr->detach;

$a=1;
print "main program start: a = $a \n";
sleep 4;
print "main program end: a = $a \n";
exit;


File "thr1_sub.pm":
sub thr1 {
sleep 1;
print "thread start: a = $a \n";
sleep 2;
$a=2;
print "thread end: a = $a \n";
return;
}; # end sub
1;

So, in fact, it's exactly the same program, but split up in two files.
(if you have a lot of threads, this is a lot easier to manage then to
have everything in one file).

But, if I run this code, I get this:

[EMAIL PROTECTED]:~/devel/perl/threads> ./thr1.pl
main program start: a = 1
thread start: a =
thread end: a = 2
main program end: a = 1

So, the variable "a" is not shared anymore!


I have the same problem when I use "do" or "use".



The system is a linux Suse 10.1 (kernel 2.6.16.13-4-default), the
version of perl is "v5.8.8 built for i586-linux-thread-multi". The
thread-library is threads-1.36 from cpan.
(I have the same problem with a rhen3 with perl 5.8.0.).



Cheerio! Kr. Bonne.

Reply via email to