[Chicago-talk] A question on threading

me at heyjay.com me at heyjay.com
Sat Jun 5 17:32:45 CDT 2004


I'm getting unpredictable (at least to me) result.  
If I run the code below, repeatedly, every so often I get:

[o10]:~> ./thread
Invalid value for shared scalar at ./thread line 25.
A thread exited while 2 threads were running.

the other times it works as expected.  I don't see what's
wrong with the code

All I'm trying to do is start a new thread print out a shared value
then stop the thread, and have it stop nicely.

I made an example that demonstrates the problem (should be 
cut/paste runnable as is)

[o10]:~> cat ./thread


package Reader;

use 5.008003;
use strict;
use warnings;

use base qw(Class::Accessor);
use threads;
use threads::shared;

__PACKAGE__->mk_accessors( qw/_run _thread/ );

sub start {

    my $self = shift;

    my $run : shared = 1;
    my $t : shared;


    my $sub = \&loop;
    $t = threads->new($sub, $self);

sub stop {
    my $self = shift;
    ${$self->_run} = 0;

sub loop {

    my $self = shift;

    while (${$self->_run}) {
        print "running: ", ${$self->_run}, " \n";

package main;

my $reader = Reader->new();


More information about the Chicago-talk mailing list