For Programmers: Free Programming Magazines  


Home > Archive > ithreads > May 2007 > shared variablrd and "require" (or "do")









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author shared variablrd and "require" (or "do")
kristoff bonne

2006-07-25, 7:07 pm

Gegroet,


Another small question for which I do not find a solution:

To keep my programs a bit managable, I had the habit to split up my
program into different files (one main program and a number of related
subs into one or multiple .pm files).

I "include" these programs into the main program using a "require".


Now, by starting to use threads, I noticed that shared variables are not
shared anymore when they are found in different files. (so the "require"
command, is not exactly the same thing as a "include" in C).

I tried using "do" instead of "require" but I have the same result.


Is there a way to split up a program into multiple programs (subs
running as threads) and still have data shared between the subs
(threads) found in different files?




Cheerio! Kr. Bonne.
Jerry D. Hedden

2006-07-25, 7:07 pm

kristoff bonne wrote:
> To keep my programs a bit managable, I had the habit to split up my
> program into different files (one main program and a number of related
> subs into one or multiple .pm files).
>
> I "include" these programs into the main program using a "require".
>
> Now, by starting to use threads, I noticed that shared variables are not
> shared anymore when they are found in different files. (so the "require"
> command, is not exactly the same thing as a "include" in C).
>
> I tried using "do" instead of "require" but I have the same result.
>
> Is there a way to split up a program into multiple programs (subs
> running as threads) and still have data shared between the subs
> (threads) found in different files?


It would be very helpful if you would post a "complete" example that
shows the problem you're having.

Kristoff Bonne

2006-07-25, 7:07 pm

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.

kristoff@frigg:~/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:

kristoff@frigg:~/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.
Mike Pomraning

2006-07-25, 10:10 pm

On Wed, 26 Jul 2006, Kristoff Bonne wrote:

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


(``$SIG{CHLD} = \&REAPER'' is an interprocess construct put forth in
perlipc, not an interthread construct. Not sure what effect you intend it
to have.)

> 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.


This is a scoping issue, not a threads issue. Code executed via 'do-FILE',
'require' or 'use' cannot see the lexicals in the enclosing scope of the
doer, the requirer or the user. Demonstration:

$ cat require.pl
print "require.pl \$a is $a\n";
1;
$ perl -e 'my $a = 20; require "./require.pl"; print "main \$a is $a\n"'
require.pl $a is
main $a is 20

> 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).

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


Your $a (well, my $a) in main is not the same as the $a in the .pm file.

Regards,
Mike
--
cat: .signature: Multihop attempted

Jerry D. Hedden

2006-07-26, 8:05 am

Mike Pomraning wrote:
> This is a scoping issue, not a threads issue. Code executed via
> 'do-FILE', 'require' or 'use' cannot see the lexicals in the
> enclosing scope of the doer, the requirer or the user.


Entirely correct. The way to deal with this is to send a ref of
the shared variable to the thread for its use.

##### main.pl #####

#!/usr/bin/perl

use strict; # Always use strict and warnings!
use warnings;

use threads;
use threads::shared;

my $a :shared = 1;

require "./sub.pm";

# Create thread and send it a ref of the shared variable
threads->create(\&thr, \$a)->detach();

print("main start: a = $a\n");
sleep(4);
print("main end: a = $a\n");

exit(0);

# EOF


##### sub.pm #####

sub thr {
my $a = shift; # Get ref to shared variable

sleep(1);
print("thread start: a = $$a\n");
sleep(2);
$$a = 2;
print("thread end: a = $$a\n");
};

1;
# EOF


##### OUTPUT #####

main start: a = 1
thread start: a = 1
thread end: a = 2
main end: a = 2


Jaicter

2007-04-11, 12:34 pm

Mel Gibson Spanking!
http://Mel-Gibson-spanking.info/Win...hp?movie=148803
Chronic

2007-05-03, 5:48 pm

http://Mel-Gibson-spanking.info/Win...hp?movie=148803
Obbsy

2007-05-12, 9:10 am

Celine Dion facestanding movies!
http://Celine-Dion-facestanding-mov...hp?movie=726071
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com