So, my question is, what mechanism could be used to avoid that
This is quite possibly a non-helpful response, but I think that "blessing" the new SV into package NULL is one way of avoiding the issue.
This effectively means that when the SV goes out of scope, it gets garbage-collected, but the object it points to survives (because DESTROY wasn't called). Therefore, any other SV's that point to the same object are still valid.
This also effectively means that the programmer has to take charge of determining if/when these objects are DESTROY()ed - or run the risk of memory leaks.
For some reason that I've never understood, I provided in Math::MPFR the capability of creating such unblessed objects and it does seem to me to provide a way of avoiding the problem. With an object blessed into package Math::MPFR:
use warnings;
use strict;
use threads;
use Math::MPFR qw(:mpfr);
my ($x, $inex) = Rmpfr_init_set_d(11.625, MPFR_RNDN);
my $thr1 = threads->create(
sub {
print Rmpfr_get_d($x, MPFR_RNDN), "\n";
return 23;
} ); ## Line 12
my $res = $thr1->join();
print $res;
__END__
OUTPUTS:
11.625
Free to wrong pool 4ab7e0 not 49aee0 at threads.pl line 12.
With the unblessed object:
use warnings;
use strict;
use threads;
use Math::MPFR qw(:mpfr);
my ($x, $inex) = Rmpfr_init_set_d_nobless(11.625, MPFR_RNDN);
my $thr1 = threads->create(
sub {
print Rmpfr_get_d($x, MPFR_RNDN), "\n";
return 23;
} );
my $res = $thr1->join();
Math::MPFR::DESTROY($x);
print $res;
__END__
OUTPUTS:
11.625
23
Maybe that's an overly simplistic and/or irrelevant demo. (My knowledge of threads is very week.)
Cheers,
Rob
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.