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";
}
-
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.
|