multithreading - Check number of alive threads -
i implementing boss-worker crew thread model in perl. due exceptions, worker thread exit abnormally.
once worker threads die(abnormal exit), jobs stucked, there no thread process workers_fn , boss thread keep enquing jobs.
thread 2 terminated abnormally: illegal division 0 @ threading.pl line 20. thread 3 terminated abnormally: illegal division 0 @ threading.pl line 20. thread 4 terminated abnormally: illegal division 0 @ threading.pl line 20. thread 5 terminated abnormally: illegal division 0 @ threading.pl line 20. thread 6 terminated abnormally: illegal division 0 @ threading.pl line 20. thread 7 terminated abnormally: illegal division 0 @ threading.pl line 20. thread 8 terminated abnormally: illegal division 0 @ threading.pl line 20. thread 9 terminated abnormally: illegal division 0 @ threading.pl line 20. thread 10 terminated abnormally: illegal division 0 @ threading.pl line 20. thread 11 terminated abnormally: illegal division 0 @ threading.pl line 20. at point of time, how can detect accurately, number of alive workers thread?
#!/usr/bin/perl use strict; use warnings; use carp; use diagnostics; use threads; use thread::queue; use data::dumper; use constant { workers_cnt => 10, }; $queue= new thread::queue(); sub workers_fn{ #producing exceptions deliberately test case $random_number = int(time); if($random_number %2==0 || $random_number %3==0 || $random_number %5==0){ print "crashed:".5/($random_number %2); print "\n"; } while (my $query = $queue->dequeue) { print "deq(".threads->tid.") :$query\n"; } } sub master_fn(){ $totalentry=10000; while($totalentry -- > 0){ $query = localtime; $queue->enqueue($query); print "enq(".threads->tid.") :$query\n"; sleep(1); #check worker threads alive { #how check number of alive threads????????????? } } } $master_th=threads->create(\&master_fn); @worker_th=map threads->create(\&workers_fn), 1..workers_cnt; $master_th->join; $queue->enqueue(undef) 1..workers_cnt; (@worker_th){ $_->join;
my $running = threads->list(threads::running); $ perl -mthreads -mthreads::shared -mtime::hires=sleep -e' $hold : shared; { lock($hold); async { sleep 1; die "ack" }; async { lock($hold) }; while (1) { $running = threads->list(threads::running); $running; last if $running < 2; sleep 0.1; } } $_->join threads->list; ' 2 2 2 2 2 2 2 2 2 2 thread 1 terminated abnormally: ack @ -e line 6. 1
Comments
Post a Comment