home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl560.zip / ext / Thread / lock.t < prev    next >
Text File  |  1999-07-20  |  535b  |  28 lines

  1. use Thread;
  2.  
  3. $level = 0;
  4.  
  5. sub worker
  6. {
  7.     my $num = shift;
  8.     my $i;
  9.     print "thread $num starting\n";
  10.     for ($i = 1; $i <= 20; $i++) {
  11.     print "thread $num iteration $i\n";
  12.     select(undef, undef, undef, rand(10)/100);
  13.     {
  14.         lock($lock);
  15.         warn "thread $num saw non-zero level = $level\n" if $level;
  16.         $level++;
  17.         print "thread $num has lock\n";
  18.         select(undef, undef, undef, rand(10)/100);
  19.         $level--;
  20.     }
  21.     print "thread $num released lock\n";
  22.     }
  23. }
  24.  
  25. for ($t = 1; $t <= 5; $t++) {
  26.     new Thread \&worker, $t;
  27. }
  28.