<?xml version="1.0" encoding="windows-1252"?>
<node id="973763" title="Re: About self-invoked threads into class" created="2012-06-01 09:58:04" updated="2012-06-01 09:58:04">
<type id="11">
note</type>
<author id="888573">
Eliya</author>
<data>
<field name="doctext">
&lt;blockquote&gt;
&lt;i&gt; but function stop_thread cannot execute functions $inter-&gt;{THR}-&gt;kill('TERM') and ...&lt;/i&gt;
&lt;/blockquote&gt;
&lt;p&gt; Your immediate problem is that you're trying to call the kill method on the value &lt;/p&gt;
&lt;c&gt;
    ref threads-&gt;create(...)
&lt;/c&gt;
&lt;p&gt; which is the string "threads", stored in &lt;c&gt;$inter-&gt;{THR}&lt;/c&gt;, instead of the thread itself, i.e. what &lt;c&gt;threads-&gt;create(...)&lt;/c&gt; returns.&amp;nbsp; However, when trying to fix the problem by getting rid of the "ref" &lt;/p&gt;
&lt;c&gt;
    $inter-&gt;{THR} = threads-&gt;create(sub{$thread-&gt;(\$inter-&gt;{INT1})});
&lt;/c&gt;
&lt;p&gt; you'd get an &lt;c&gt;"Invalid value for shared scalar"&lt;/c&gt; error... &lt;/p&gt;
&lt;p&gt; So, you probably want to take the thread variable out of the shared data structure, e.g. modify your code like this &lt;/p&gt;
&lt;c&gt;
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-&gt;{INT1}    = "internal 1";
    $inter-&gt;{INT2}    = "internal 2";
    $inter-&gt;{INT3}    = "internal 5"; 
    my $self = {};
    $self-&gt;{inter} = $inter;
    return bless $self, $class;
}

sub stop_thread {
    my $self = shift;
    $self-&gt;{THR}-&gt;kill('TERM');
    $self-&gt;{THR}-&gt;join();
}

sub starter {
    my $self = shift;
    my $inter = $self-&gt;{inter};
    $self-&gt;{THR} = threads-&gt;create(sub{$thread-&gt;(\$inter-&gt;{INT1})});
    return    ( 1 );
}

sub set_internal {
    my $self = shift;
    my $inter = $self-&gt;{inter};
    if( @_ ) {
        my ( $given_key, $given_value ) = @_ if @_;
        $inter-&gt;{$given_key} = $given_value;
        return    ( 1 );
    }
    return    ( 0 );
}

sub printer {
    my $self = shift;
    my $inter = $self-&gt;{inter};
    foreach my $key ( keys %{$inter} ) {
        print $key, " - ", $inter-&gt;{$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;
&lt;/c&gt;
&lt;p&gt; The idea is to have a "wrapper" hash/object, which stores &lt;/p&gt;
&lt;c&gt;
    $self-&gt;{inter}   # access to shared data
    $self-&gt;{THR}     # access to thread
&lt;/c&gt;
&lt;p&gt; With these modifications, the code works fine for me (if I'm understanding your intentions correctly). &lt;/p&gt;</field>
<field name="root_node">
973751</field>
<field name="parent_node">
973751</field>
</data>
</node>
