Hello dear,
I'm getting troubles in XS modules when i use threads.
In addition to MY_CXT i need a clean destructor for system resources like file handles, that are not clonable.
The following example does exactly the same (bad thing) like my XS modules do. A new thread is created before the previous thread detachs. CLONE is called correctly, but DESTROY is called to less.
Is it a bug or do i wrong? Please help.
#!/usr/local/bin/perl
use threads;
use Time::HiRes qw/usleep/;
while( 1 ) {
my $class = Class->new();
threads->create( \&thread_sub, $class );
usleep 200_000;
}
sub thread_sub {
my( $class ) = @_;
usleep 400_000;
threads->self->detach;
}
1;
package Class;
use threads::shared;
our %MY_CXT : shared = ();
our $CXT_ID : shared = 0;
sub new {
my $pkg = shift;
my $this = {};
$this->{'shared'} = &share( {} ),
$this->{'shared'}{'refcnt'} = 1;
lock( %MY_CXT );
$this->{'id'} = ++ $CXT_ID;
$MY_CXT{$this->{'id'}} = $this->{'shared'};
bless $this, $pkg;
}
sub CLONE {
my $this = shift; # not applicable
lock( %MY_CXT );
while( my( $k, $v ) = each %MY_CXT ) {
$v->{'refcnt'} ++;
print "CLONE: id $k, refcnt $v->{'refcnt'}\n";
}
}
sub DESTROY {
my $this = shift;
my $shared = $this->{'shared'};
$shared->{'refcnt'} --;
print "DESTROY: id $this->{'id'}, refcnt $shared->{'refcnt'}\n";
if( $shared->{'refcnt'} <= 0 ) {
lock( %MY_CXT );
delete $MY_CXT{$this->{'id'}};
}
}
1;
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.