thread problem

Discussion in 'Perl Misc' started by Dave Saville, Sep 6, 2013.

  1. Dave Saville

    Dave Saville Guest

    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;
    }
    }
    }
    --
    Regards
    Dave Saville
     
    Dave Saville, Sep 6, 2013
    #1
    1. Advertising

  2. "Dave Saville" <> writes:

    [...]

    > 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?


    Because you didn't declare it as shared variable.

    ----------
    use strict;
    use warnings;
    use threads;
    use threads::shared;
    my $foo : shared = 'foo';
    threads->create({'void' => 1}, 'try');
    sleep 2;
    print "$foo - done\n";
    sleep 5;
    sub try
    {
    threads->detach();
    $foo = 'bar';
    sleep 5;
    return;
    }
    ---------

    [...]

    > 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.


    Using threads in this way, especially perl ithreads, is almost
    certainly not a good idea. What's your main thread doing? If it is
    'handling other I/O', you should consider using select or a module
    providing access to a similar system call. The IO::poll
    implementations is beyond braindead. OTOH, IO::EPoll is only 'slightly
    bizarre', eg, it uses 3 hashes to map file handles to various things
    instead of putting all "per filehandle" attributes into a suitable
    object and let the kernel maintain a pointer to that, something which
    also works in perl 5 because its automatich memory management system
    wasn't built on the wrong assumption that pointer to objects cannot
    exist outside of the address space of the process which created them.
     
    Rainer Weikusat, Sep 6, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,586
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. Manisha
    Replies:
    2
    Views:
    569
  3. NaeiKinDus
    Replies:
    1
    Views:
    605
    Jack Klein
    Apr 14, 2007
  4. NaeiKinDus
    Replies:
    3
    Views:
    640
    James Kanze
    Apr 15, 2007
  5. Replies:
    3
    Views:
    178
Loading...

Share This Page