Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found