perlmod has a nice description of how your modules can play nice with threads.
It describes the
CLONE_SKIP routine which you can add to your module to prevent cloning across threads.
If you only want to not clone one of many instances, you could create a subclass
Class::Foo::NoClone which is empty save for a
sub CLONE_SKIP { 1 } and a
@ISA = ('Class::Foo'); and rebless the specific instance into that.
Also, are you sure that it won't clone the second
$x? Its been a while since I've used threads. Where in the docs does it say that? I would assume they would be treated equally, both being lexical variables and all.
Update: proof that the second $x is cloned
use threads;
use strict;
use warnings;
use PadWalker qw(peek_sub);
sub thr {
my $xref = peek_sub(\&run_test)->{'$x'};
print "\$xref in thr: $xref\n";
}
sub run_test {
my $x = 1;
my $xref = \$x;
print "\$xref before thr: $xref\n";
threads->create(\&thr)->join;
$xref = \$x;
print "\$xref after thr: $xref\n";
}
run_test;
Output (on perl v5.8.8 built for x86_64-linux-gnu-thread-multi):
$xref before thr: SCALAR(0x6c51a0)
$xref in thr: SCALAR(0x7584a0)
$xref after thr: SCALAR(0x6c51a0)
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.