in reply to multi-threaded questions
#!/usr/bin/perl use threads; use Thread::Queue; use threads::shared; our $queue = Thread::Queue->new; my $test = "0"; my $stop_var : shared = 0; for (1..30){ $thr = threads->new(\&thread_sub1); } for (1..30){ $thr1 = threads->new(\&thread_sub); } foreach $thr ( threads->list ) { $thr->join; } foreach $thr1 ( threads->list ) { $thr1->join; } sub thread_sub { my $tid = threads->self->tid(); my $sec=1; until ($sec == 0) { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) += localtime(time); # print "$sec\n"; } lock($stop_var); if ($stop_var ==0) { $stop_var = 1; for (1..30){ $queue->enqueue($tid); } } } sub thread_sub1 { my $tid = threads->self->tid(); print "Order thread number $tid spawned\n"; $action1 = $queue->dequeue; print "I have gotten through the queue my lord: $action1\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: multi-threaded questions
by zentara (Cardinal) on Dec 09, 2006 at 12:31 UTC | |
|
Re^2: multi-threaded questions
by renodino (Curate) on Dec 09, 2006 at 21:20 UTC |