a simple thread testing program produces some very strange results:could you please tell me what the

Discussion in 'Perl Misc' started by Asfand Yar Qazi, Nov 26, 2003.

  1. #!/usr/local/bin/perl -w

    package Thread_Maker;

    use strict;

    sub new()
    {
    my $proto = shift();
    my $class = ref($proto) || $proto;
    my $self = [];
    bless($self, $class);
    return $self;
    }

    sub joinall()
    {
    my $self = shift;
    foreach my $thread (@$self) {
    print("Joining thread\n");
    $$thread->join();
    }
    }

    sub add(\@\&@)
    {
    my ($self, $funcref, @args) = @_;
    my $tmpthread = threads->new($funcref, @args);
    push(@$self, \$tmpthread);
    }

    1;

    package main;

    use Config;
    use threads;
    use threads::shared;
    use Gtk;
    use strict;

    $Config{useithreads} or die "Recompile Perl with threads to run this
    program.";

    my $a : shared = 0;

    sub sub1($\$)
    {
    my ($sleeptime, $var) = @_;
    sleep($sleeptime);
    ++$$var;
    }

    sub printer(\$)
    {
    my ($var) = @_;
    my $oldvar = $var;
    while($$var != 2) {
    while($$var == $oldvar) {}
    $oldvar = $$var;
    print('Variable = ', $$var, "\n");
    };
    print("Now exiting\n");
    }

    my $tmaker = new Thread_Maker;

    $tmaker->add(\&printer, \$a);
    $tmaker->add(\&sub1, 1, \$a);
    $tmaker->add(\&sub1, 2, \$a);

    $tmaker->joinall();

    --OUTPUT--

    Variable = 0
    Joining thread
    Variable = 1
    Variable = 2
    Now exiting
    Joining thread
    Joining thread
    Scalars leaked: 1
    Scalars leaked: 1
    Scalars leaked: 1

    --END OUTPUT--

    What's with the 'Scalars leaked: 1' stuff?







    --
    http://www.it-is-truth.org/
     
    Asfand Yar Qazi, Nov 26, 2003
    #1
    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. harryajh
    Replies:
    9
    Views:
    656
    harryajh
    Mar 27, 2008
  2. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,376
  3. Mark B
    Replies:
    1
    Views:
    428
    Göran Andersson
    Feb 11, 2009
  4. a s
    Replies:
    16
    Views:
    4,761
    JustJohn
    Mar 8, 2011
  5. Max Norman
    Replies:
    5
    Views:
    147
Loading...

Share This Page