title: Perl threads not printing correctly
body: <p>If there are explicit examples in another post please let me know.  I am having problems with interleaved printing from my threads.  I am trying to control my threads by using a shared variable across all threads.  The pseudo code below highlights the pieces of my code giving me problems.  I have tried everything to make the threads wait their turns to print.  Right now only a few output lines are being destroyed. </p>
  
  <pre><code>#!/usr/bin/perl                                                                                                                                
  use threads;
  use threads::shared;
  
  my $PRINTFLAG :shared = 1;
  
  Run_Threads();
  
  sub Do_stuff{
  
      lock($PRINTFLAG);
      cond_wait($PRINTFLAG) until $PRINTFLAG == 1;
      $PRINTFLAG = 0;
      print "$working\n";
      $PRINTFLAG =1;
  }
  </code></pre>
  
  <p>Sub to spawn threads.</p>
  
  <pre><code>sub Run_Threads{
  
      my @threads;
  
      for (my $i = 1; $i &lt;= 5; $i++){
          push @threads, threads-&gt;create(\&amp;Do_stuff);
      }
  
      foreach (@threads){
          $_-&gt;join;
      }
  }
  </code></pre>