D
Dave Saville
I am having a problem with threads on a Raspberry Pi. (Note - have
never used threads in perl. Only C)
use strict;
use warnings;
use threads;
use threads::shared;
my $foo = 'foo';
threads->create({'void' => 1}, 'try');
sleep 2;
print "$foo - done\n";
sleep 5;
sub try
{
threads->detach();
$foo = 'bar';
sleep 5;
return;
}
Why isn't $foo changing in the main thread? I have tried passing $foo
as an argument to try both as itself and as a scalar ref. No way can I
get $foo to change value in main.
What I am trying to do is use a thread to essentially tail a pipe and
raise a flag in main if it sees a certain string appear.The main
thread can't tail it because I don't want to block. So my "real* sub
is:
sub get_title
{
threads->detach();
my $ref = shift;
open my $FIFO, '<', '/home/pi/fifo_stdout' or die "Can't open
fifo_stdout $!";
while ( <$FIFO> )
{
if ( m{^StreamTitle='([^']*)'} )
{
$$ref = $1;
}
}
}
never used threads in perl. Only C)
use strict;
use warnings;
use threads;
use threads::shared;
my $foo = 'foo';
threads->create({'void' => 1}, 'try');
sleep 2;
print "$foo - done\n";
sleep 5;
sub try
{
threads->detach();
$foo = 'bar';
sleep 5;
return;
}
Why isn't $foo changing in the main thread? I have tried passing $foo
as an argument to try both as itself and as a scalar ref. No way can I
get $foo to change value in main.
What I am trying to do is use a thread to essentially tail a pipe and
raise a flag in main if it sees a certain string appear.The main
thread can't tail it because I don't want to block. So my "real* sub
is:
sub get_title
{
threads->detach();
my $ref = shift;
open my $FIFO, '<', '/home/pi/fifo_stdout' or die "Can't open
fifo_stdout $!";
while ( <$FIFO> )
{
if ( m{^StreamTitle='([^']*)'} )
{
$$ref = $1;
}
}
}