Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
but function stop_thread cannot execute functions $inter->{THR}->kill('TERM') and ...

Your immediate problem is that you're trying to call the kill method on the value

ref threads->create(...)

which is the string "threads", stored in $inter->{THR}, instead of the thread itself, i.e. what threads->create(...) returns.  However, when trying to fix the problem by getting rid of the "ref"

$inter->{THR} = threads->create(sub{$thread->(\$inter->{INT1})});

you'd get an "Invalid value for shared scalar" error...

So, you probably want to take the thread variable out of the shared data structure, e.g. modify your code like this

package HardThread; use strict; use warnings; use threads; use threads::shared; use vars qw( $internal ); ## Variable declaration for internal functions my ( $thread ); sub new { my $class = shift; my $inter : shared = shared_clone({}); $inter->{INT1} = "internal 1"; $inter->{INT2} = "internal 2"; $inter->{INT3} = "internal 5"; my $self = {}; $self->{inter} = $inter; return bless $self, $class; } sub stop_thread { my $self = shift; $self->{THR}->kill('TERM'); $self->{THR}->join(); } sub starter { my $self = shift; my $inter = $self->{inter}; $self->{THR} = threads->create(sub{$thread->(\$inter->{INT1})}); return ( 1 ); } sub set_internal { my $self = shift; my $inter = $self->{inter}; if( @_ ) { my ( $given_key, $given_value ) = @_ if @_; $inter->{$given_key} = $given_value; return ( 1 ); } return ( 0 ); } sub printer { my $self = shift; my $inter = $self->{inter}; foreach my $key ( keys %{$inter} ) { print $key, " - ", $inter->{$key}, "\n"; } } $thread = sub { local *internal = $_[0]; my $control = 1; $SIG{'TERM'} = sub { $control = 0; }; while( $control == 1 ) { print "Thread shows internal value: ", $internal, "\n"; sleep 2; } return ( 1 ); }; 1;

The idea is to have a "wrapper" hash/object, which stores

$self->{inter} # access to shared data $self->{THR} # access to thread

With these modifications, the code works fine for me (if I'm understanding your intentions correctly).

In reply to Re: About self-invoked threads into class by Eliya
in thread About self-invoked threads into class by Omniperl

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [erix]: This sad, embarrassing wreck of a man

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (7)
    As of 2018-07-17 21:48 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (378 votes). Check out past polls.