In following example i'm testing several cases where shared object is assigned to shared hash. I observerd that if i use
refcount on object reference or
weaken on reference inside the shared hash
after assignment this object fails to destroy.
package Foo;
use threads;
use threads::shared;
sub new {
my ($class, $share) = @_;
print "Init\n";
return $share ? bless shared_clone {} : bless {};
}
DESTROY {
print "Destroy\n";
}
package main;
use common::sense;
use threads;
use threads::shared;
use Scalar::Util qw/weaken/;
use Devel::Refcount qw(refcount);
say "Test 1 (using two refcounts)";
{
my %BAR :shared;
my $foo = Foo->new(1);
say refcount($foo);
$BAR{foo} = $foo;
say refcount($foo);
}
say "Test 2 (single refcount)";
{
my %BAR :shared;
my $foo = Foo->new(1);
say refcount($foo);
$BAR{foo} = $foo;
#say refcount($foo);
}
say "Test 3 (no weaken)";
{
my %BAR :shared;
my $foo = Foo->new(1);
$BAR{foo} = $foo;
# weaken($BAR{foo});
}
say "Test 4 (with weaken)";
{
my %BAR :shared;
my $foo = Foo->new(1);
$BAR{foo} = $foo;
weaken($BAR{foo});
}
My output is:
Test 1 (using two refcounts)
Init
1
1
Test 2 (single refcount)
Init
1
Destroy
Test 3 (no weaken)
Init
Destroy
Test 4 (with weaken)
Init
My main concern is that i can't use
weaken on my shared objects inside a hash.
I'm using:
ActiveState Perl 5.12.4
threads 1.86
threads::shared 1.40
Devel::Refcount 0.09
Scalar::Util 1.23
Is this a bug? And is it possible to keep weak references to shared objects inside a hash?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.