Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Here's some working queuer/consumer code based on Thread::Queue. Hope it helps.~
#!/usr/bin/perl use threads; use threads::shared; use Thread::Queue; my $q : shared; use constant FINISH => 'finish'; main(); exit 0; sub main { $q = Thread::Queue->new(); my $queuer = threads->create('queuer', 10); my $worker = threads->create('worker'); $queuer->join; $worker->join; print "main thread finished\n"; } sub queuer { my $numItems = shift; my $str = "foo"; while ($numItems-- > 0) { print "queueing $str\n"; $q->enqueue($str); $str++; sleep 1; } $q->enqueue(FINISH); print "queuer finished\n"; } sub worker { DOING_WORK: while (1) { while (my $pending = $q->pending) { my $str = $q->dequeue; print "worker found $pending items, got $str\n"; last DOING_WORK if $str eq FINISH; } sleep 3; } print "worker finished\n"; }

In reply to Re: Thread communication by jbert
in thread Thread communication by libvenus

Title:
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-19 01:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found