Help for this page

Select Code to Download


  1. or download this
    use threads;
    use strict;
    
  2. or download this
    # I added this to make sure we see all printing on time
    $| ++;
    
  3. or download this
    # constants
    my $workers = 1; #number of simultaneous workers
    ...
        while (1) {
            @running_threads = threads->list;
            if (scalar(@running_threads) < $workers) {
    
  4. or download this
            #I added this, and it only shows ONCE 
            print "creating new threads\n";
    
  5. or download this
            
        # add a new worker
                $count++;
                my $thread = threads->new(\&worker, $count);
            }
    
  6. or download this
            # I commented out this, with this flushing the output all the 
    +time, I cannot see anything useful
            #list_threads();
    
  7. or download this
            
        }
    }
    ...
    sub worker {
        my $thread_num = $_[0];
        print "---I am thread $thread_num!\n";
    
  8. or download this
        #this is simply a lie
        print "+++Thread $thread_num gone!\n";
        #this hangs this worker thread itself
        eval(((threads->self)->join));
    
  9. or download this
            
    }