Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2023-06-03 10:33 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (12 votes). Check out past polls.