Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^5: What is the correct way to finish multithreaded program?

by Anonymous Monk
on May 05, 2014 at 13:08 UTC ( #1085064=note: print w/ replies, xml ) Need Help??


in reply to Re^4: What is the correct way to finish multithreaded program?
in thread What is the correct way to finish multithreaded program?

Here you go ... just because queue is empty, doesn't mean threads are joinable

#!/usr/bin/perl -- use strict; use warnings; use Time::HiRes qw/ usleep /; use threads; use Thread::Queue; #~ $|=1; STDOUT->autoflush(1); my $q = Thread::Queue->new(); my $pq = Thread::Queue->new(); my $config = { number_of_threads => 10 }; #~ my @threads = map { threads->create( \&worker, $_ ) } ( 1 .. $confi +g->{number_of_threads} ); #~ push @threads, threads->create( \&controller ); my @threads = map { threads->create( \&worker, $q, $pq ) } ( 1 .. $con +fig->{number_of_threads} ); push @threads, threads->create( \&controller, $q, $pq ); #~ my $mech = WWW::Mechanize->new(); #~ $mech->agent_alias("Windows IE 6"); #~ $mech->get( $config->{start_url} ); #~ my @states = find_states($mech); #~ my @states = qw/ my @states = split /\s+/, q/ I can't seem to face up to the facts. I'm tense and nervous and I... can't relax. I can't sleep, cause my bed's on fire. Don't touch me I'm a real live wire. /; foreach my $state (@states) { #~ process_state( $state ); process_state( $q, $state ); } while ( my $pend = $q->pending() ) { print "q pending $pend\n"; sleep 1; } #~ foreach ( 1 .. $config->{number_of_threads} ) { #~ $q->enqueue(undef); #~ } $q->enqueue(undef) for @threads; while ( my $pend = $pq->pending() ) { print "pq pending $pend\n"; sleep 1; } $pq->enqueue(undef); foreach my $thr (@threads) { if ( $thr->is_joinable() ) { $thr->join(); } else { printf "AWW FUDGE %s %s\n", $thr, $thr->tid,; #~ $thr->join; } } sub worker { my( $q, $pq ) = @_; while ( my $org = $q->dequeue() ) { #~ printf "worker tid(%s)(%s)\n", threads->tid, $org; printf "worker tid(%s)q(%s)\n", threads->tid, $org; #~ parse_org( $org, $mech ); #~ usleep( 1000 * rand 3 ); #~ usleep( 1000 * rand 13 ); sleep 4; $pq->enqueue($org); } return; } sub controller { my( $q, $pq ) = @_; while ( my $org = $pq->dequeue() ) { #~ printf "controller tid(%s)(%s)\n", threads->tid, $org; printf "controller tid(%s)pq(%s)\n", threads->tid, $org; usleep( 1000 * rand 3 ); } return; } sub process_state { my( $q , $state ) = @_; #~ usleep( 1000 * 1/9 ); print "q enqueue $state\n"; $q->enqueue( $state ); #~ usleep( 1000 * 1/9 ); #~ sleep 2; return; } __END__ q enqueue q enqueue I worker tid(2)q(I) q enqueue can't worker tid(3)q(can't) q enqueue seem worker tid(4)q(seem) q enqueue to worker tid(5)q(to) q enqueue face worker tid(6)q(face) q enqueue up worker tid(7)q(up) q enqueue to worker tid(8)q(to) q enqueue the worker tid(9)q(the) q enqueue facts. worker tid(10)q(facts.) q enqueue I'm q enqueue tense q enqueue and q enqueue nervous q enqueue and q enqueue I... q enqueue can't q enqueue relax. q enqueue I q enqueue can't q enqueue sleep, q enqueue cause q enqueue my q enqueue bed's q enqueue on q enqueue fire. q enqueue Don't q enqueue touch q enqueue me q enqueue I'm q enqueue a q enqueue real q enqueue live q enqueue wire. q pending 24 q pending 24 q pending 24 q pending 24 controller tid(11)pq(I) worker tid(2)q(I'm) worker tid(3)q(tense) worker tid(4)q(and) worker tid(5)q(nervous) worker tid(6)q(and) worker tid(7)q(I...) worker tid(8)q(can't) worker tid(9)q(relax.) worker tid(10)q(I) q pending 15 controller tid(11)pq(can't) controller tid(11)pq(seem) controller tid(11)pq(to) controller tid(11)pq(face) controller tid(11)pq(up) controller tid(11)pq(to) controller tid(11)pq(the) controller tid(11)pq(facts.) q pending 15 q pending 15 q pending 15 controller tid(11)pq(I'm) worker tid(2)q(can't) worker tid(3)q(sleep,) worker tid(4)q(cause) worker tid(5)q(my) worker tid(6)q(bed's) worker tid(7)q(on) worker tid(8)q(fire.) worker tid(9)q(Don't) worker tid(10)q(touch) q pending 6 controller tid(11)pq(tense) controller tid(11)pq(and) controller tid(11)pq(nervous) controller tid(11)pq(and) controller tid(11)pq(I...) controller tid(11)pq(can't) controller tid(11)pq(relax.) controller tid(11)pq(I) q pending 6 q pending 6 q pending 6 controller tid(11)pq(can't) worker tid(2)q(me) worker tid(3)q(I'm) worker tid(4)q(a) worker tid(5)q(real) worker tid(6)q(live) worker tid(7)q(wire.) pq pending 8 controller tid(11)pq(sleep,) controller tid(11)pq(cause) controller tid(11)pq(my) controller tid(11)pq(bed's) controller tid(11)pq(on) controller tid(11)pq(fire.) controller tid(11)pq(Don't) controller tid(11)pq(touch) AWW FUDGE threads=SCALAR(0xb62604) 2 AWW FUDGE threads=SCALAR(0xb62624) 3 AWW FUDGE threads=SCALAR(0xb62644) 4 AWW FUDGE threads=SCALAR(0xb62664) 5 AWW FUDGE threads=SCALAR(0xb62684) 6 AWW FUDGE threads=SCALAR(0xb626a4) 7 Perl exited with active threads: 6 running and unjoined 0 finished and unjoined 0 running and detached

This reliably exits with 6 running and unjoined on my old machine, YMMV


Comment on Re^5: What is the correct way to finish multithreaded program?
Download Code
Re^6: What is the correct way to finish multithreaded program?
by Gangabass (Priest) on May 06, 2014 at 03:04 UTC
    Thank you for nice explanation! Now I got it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1085064]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-10-02 12:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (56 votes), past polls