ref threads->create(...)
####
$inter->{THR} = threads->create(sub{$thread->(\$inter->{INT1})});
####
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;
####
$self->{inter} # access to shared data
$self->{THR} # access to thread