Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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


In reply to Re^5: What is the correct way to finish multithreaded program? by Anonymous Monk
in thread What is the correct way to finish multithreaded program? by Gangabass

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (6)
    As of 2014-12-29 12:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (187 votes), past polls