Re: threads::shared::share behaves differently when imported or not

[email protected] (Stas Bekman)
Newsgroups perl.ithreads
Organization Hope, Humanized
Message-ID <[email protected]>
Elizabeth Mattijsen wrote:
> At 14:50 +0200 7/30/03, Stas Bekman wrote:
> 
>> if I 'use threads::shared', share() works as documented:
>>
>> perl-5.8.1-ithread -le 'use threads; use threads::shared; my $x; 
>> share($x); $x = 1;'
>>
>> perl-5.8.1-ithread -le 'use threads; use threads::shared; my $x; 
>> threads::shared::share($x); $x = 1;'
>>
>> However if I 'require threads::shared', it doesn't work the same way:
>>
>> perl-5.8.1-ithread -le 'use threads; require threads::shared; my $x; 
>> threads::shared::share($x); $x = 1;'
>> Argument to share needs to be passed as ref at -e line 1.
>>
>> If I pass \$x it works.
>>
>> If I explicitly import share it doesn't.
>>
>> perl-5.8.1-ithread -le 'use threads; require threads::shared; 
>> threads::shared->import(qw(share)); my $x; share($x); $x = 1;'
>> Argument to share needs to be passed as ref at -e line 1.
>>
>> why?
> 
> 
> Maybe you're getting the dummy share sub somehow?
> 
> from threads::shared.pm
> 
> BEGIN {
>     if ($threads::threads) {
> : yadayadayada
>         *cond_wait = \&cond_wait_enabled;
>         *cond_signal = \&cond_signal_enabled;
>         *cond_broadcast = \&cond_broadcast_enabled;
>         require XSLoader;
>         XSLoader::load('threads::shared',$VERSION);
>         push @EXPORT,'bless';
>     }
>     else {
>         *share = \&share_disabled;
>         *cond_wait = \&cond_wait_disabled;
>         *cond_signal = \&cond_signal_disabled;
>         *cond_broadcast = \&cond_broadcast_disabled;
>     }
> }
> 
> : yadayadayada
> sub share_disabled          (\[$@%]) { return $_[0] }

Let's test it:

perl-5.8.1-ithread -le 'require threads; print $threads::threads; require 
threads::shared; print join "\n", \&threads::shared::share, 
\&threads::shared::share_disabled;'
1
CODE(0x80f12e8)
CODE(0x80f1384)

There are not the same. I didn't figure out yet why, but if I move the require 
parts into a BEGIN {} block everything works OK:

perl-5.8.1-ithread -le 'BEGIN {require threads; require threads::shared;}; $x; 
threads::shared::share($x);'

But I can't have them in the BEGIN block, I need to conditionally load them at 
run time. Does this ring any bells?


__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:[email protected] http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com
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.